3

一旦用户“激活”了他们的帐户,我在尝试向用户显示所有用户数据(包括当前用户)时遇到了这个问题。当侦听列表中项目的 onValue 更改,然后在回调中侦听列表的 child_added 事件时,原始“观察”项目的快照中缺少数据。

人为复制:

var Firebase = require('firebase');
var rootRef = new Firebase('YOUR-FIREBASE-URL');

var userListRef = rootRef.child('users');

// Clearing the list
userListRef.remove();

// Create a list of users with at least two key/values
var firstUserRef = userListRef.push({name: 'me', active: false});
userListRef.push({name: 'you', active: false});
userListRef.push({name: 'someone', active: false});

// listen for a change to one of the users child data
firstUserRef.child('active').once('value', function(activeData){

    // When the value change callback fires list all of the users data
    userListRef.on('child_added', function(userData){
        console.log(userData.val());
    })

}); 

控制台输出:

{ active: false }
{ active: false, name: 'you' }
{ active: false, name: 'someone' }

姓名数据去哪了?这似乎不是预期的行为,也不是同步问题,因为名称数据应该已经在本地设置。如果我们修改代码以检索整个列表对象,如下所示:

// listen for a change to one of the users child data
firstUserRef.child('active').once('value', function(activeData){

    // When the value change callback fires list all of the users data
    userListRef.on('value', function(usersData){
        console.log(usersData.val());
    })

});

包括所有数据:

{ '-J1ayCVS6B_zVterQWJh': { active: false, name: 'me' },
  '-J1ayCVUUAFP0TL77oaO': { active: false, name: 'you' },
  '-J1ayCVUUAFP0TL77oaP': { active: false, name: 'someone' } }

有人知道这里发生了什么吗?

4

1 回答 1

0

这是错误的,你将一个事件附加到另一个事件中......没有必要......

例子 :

cars (parent node)
----- -J1ayCVS6B_zVterQWJh
----- -J1ayCVUUAFP0TL77oaO
----- -J1ayCVUUAFP0TL77oaP

抓住变化

var ref = new Firebase('https://g.com/cars");

ref.on('value', function(snapshot) {
    if (snapshot.val() != null) {

        snapshot.forEach(function(itemDetails) {
            itemDetails.val().username;
            itemDetails.val().active;
        });
    }
});


//and to be serious first attach the event, then you push the records
//assume is a button_click 
var ref2 = new Firebase('https://g.com/cars");

var firstUserRef = ref2.push({name: 'me', active: false});
firstUserRef.push({name: 'you', active: false});
firstUserRef .push({name: 'someone', active: false});
于 2013-10-15T13:02:36.083 回答