react-native firebase将快照推送到阵列

时间:2018-09-08 21:47:24

标签: javascript reactjs firebase react-native firebase-realtime-database

我想将从Firebase获取的快照推送到数组,以便我可以通过它进行映射,以某种方式,对象不断覆盖数组,而不是附加到数组本身!

这是我的功能

  _getMessages = async () => {

    await firebase.database().ref('User-Message').child(this.state.fromUser).child(this.state.toUser).on('child_added', async (snapshot) => {

      let message_array = [];

      let message_id = await snapshot.key;

      let message_ref = await firebase.database().ref('Message').child(message_id).once('value', async (payload) => {

        await message_array.push(payload.val())

      })

      console.log(message_array);

    })

  }

它一直这样记录日志

[{
    toId : 'id',
    text : 'message',
    fromId : 'id',
    time : timestamp
}]
[{
    toId : 'id',
    text : 'message',
    fromId : 'id',
    time : timestamp
}]
[{
    toId : 'id',
    text : 'message',
    fromId : 'id',
    time : timestamp
}]
[{
    toId : 'id',
    text : 'message',
    fromId : 'id',
    time : timestamp
}]

我希望它位于一个数组中,以便我可以通过它映射

我该如何实现?

4 个答案:

答案 0 :(得分:0)

只需将您的消息数组放在事件侦听器之外即可。

答案 1 :(得分:0)

_getMessages = () => {
    let message_array = [];
    var isLoaded = false;
    var self = {
        onArrayChanged: () => {
            console.log(message_array);
        },
        currentArray: () => {
            return message_array;
        },
        load: ()=>{
            if(isLoaded)
                return Promise.resolve();
            return new Promise((res,rej)=>{
                firebase.database().ref('User-Message').child(this.state.fromUser).child(this.state.toUser).on('child_added', (snapshot) => {
                    let message_id = snapshot.key;
                    var promiseArray = [];
                    promiseArray.push(new Promise(res1,rej1)=>{
                        firebase.database().ref('Message').child(message_id).once('value', (payload) => {
                            message_array.push(payload.val());
                            if(isLoaded)
                                self.onArrayChanged();
                            else
                                res1();
                        });
                    });
                    Promise.all(promiseArray).then(()=>{
                        isLoaded = true; 
                        res();
                    }).catch(rej);
                });
            });
        }
    };
    return self;
  }

    // Usage

    var messages = _getMessages();
    try{
        await messages.load();
        console.log("loaded");
        console.log(messages.currentArray);
    }catch(e){

    }

答案 2 :(得分:0)

firebase.database().ref('Message').once('value') 
  .then((snapshot) => {
    const message_array= [];

     snapshot.forEach((childSnapshot) => {
       message_array.push({
        id: childSnapshot.key,
         ...childSnapshot.val()
       });
     });

答案 3 :(得分:0)

我同意维克多的答复。

_getMessages = async () => {

     await firebase.database().ref('User-Message').child(this.state.fromUser).child(this.state.toUser).on('child_added', async (snapshot) => {

           let message_array = []; // Why is the array initialized everytime on add?

           let message_id = await snapshot.key;    
           let message_ref = await  firebase.database().ref('Message').child(message_id).once('value', async (payload) => {    
              await message_array.push(payload.val())
           }) 

           console.log(message_array);
        })
}