使用React + Firebase,如何在嵌套查询中修改状态?

时间:2017-02-27 11:39:49

标签: javascript reactjs firebase firebase-realtime-database

我目前正在初始化我的状态:

getInitialState: function () {
    return {
        conversations: {},
        messages: {},
        availableConversations: {}
    }
},

如果我这样做:

convosRef.on('value', snapshot => {
        this.setState({ conversations: snapshot.val() });
    });

它按预期工作......但是:

users: {
    uid: {
        conversations: {
            conversationid: true,
            conversationid2: true
        }
    }
}

我成功获得了所需的对象:

userSnapshot.child('conversations').forEach(function (conversationKey) {
            var conversationRef = database.ref('conversations').child(conversationKey.key);
            conversationRef.on('value', function (conversationsSnapshot) {
                var conversation = conversationsSnapshot.val();
                conversationLoadedCount = conversationLoadedCount + 1;
                myObject[conversationKey.key] = conversation;
                // console.log(conversationKey.key);
                //this.state.conversations = conversation;

                if (conversationLoadedCount === conversationCount) {
                    console.log("We've loaded all conversations");

                }
            });
            this.setState({ conversations: myObject });
        });
    });

然而。我收到两个错误,但我无法影响状态: 第一个错误: `FIREBASE警告:用户回调抛出了异常。 TypeError:无法读取属性' setState' of null``

略有相似: Uncaught TypeError: Cannot read property 'setState' of null

我的代码基于这个优秀的答案而没有任何成功: Firebase two-way relationships / Retrieving data

这是在componentDidMount function

内部运行的

有关如何影响州的任何建议?

2 个答案:

答案 0 :(得分:1)

在ES6中,如果你使用胖箭头功能,这将更加清洁,并且"这个"将受约束,例如:

   userSnapshot.child('conversations').forEach((conversationKey) => {
            var conversationRef = database.ref('conversations').child(conversationKey.key);
            conversationRef.on('value', (conversationsSnapshot) => {

答案 1 :(得分:0)

我通过尝试绑定它找到了我的问题的解决方案。 每当我在bind(this)上进行呼叫时,解决方案是使用firebase.database().ref()。 这是最后的片段,希望它可以帮助像我这样的可怜的灵魂,如果有人有更好的答案或如何改善这个的解释,请告诉我:

var myObject = {};         usersRef.on(' value',function(userSnapshot){

        // First we get the qty of conversations.
        var conversationCount = userSnapshot.child('conversations').numChildren();
        // we initialize an empty counter
        var conversationLoadedCount = 0;
        // we traverse now the conversations ref with the past conversations.
        userSnapshot.child('conversations').forEach(function (conversationKey) {
            var conversationRef = database.ref('conversations').child(conversationKey.key);
            conversationRef.on('value', function (conversationsSnapshot) {
                var conversation = conversationsSnapshot.val();

                conversationLoadedCount = conversationLoadedCount + 1;
                myObject[conversationKey.key] = conversation;
                // console.log(conversationKey.key);
                this.state.conversations[conversationKey.key] = conversation;
                this.setState({ conversations: this.state.conversations });
                if (conversationLoadedCount === conversationCount) {
                    console.log("We've loaded all conversations");
                }
            }.bind(this)); // one for the usersRef
        }.bind(this)); // another one for the forEach inside 
    }.bind(this)); // the final one for the conversationRef.on...