Update value of variable from Firebase query?

前端 未结 1 1212
臣服心动
臣服心动 2021-01-28 02:25

I currently am trying to pull the value of an item from a firebase using once, and use this value to populate the var itemsList. However, although item

相关标签:
1条回答
  • 2021-01-28 02:46

    You're correct about the Firebase call, it's asynchronous and the code isn't going to wait for it to complete before logging the unpopulated itemsList.

    If you're only looking to do something simple with the data, just be sure to check that it exists before performing any action with it (and handle it like you would any async data).

    if(itemsList){
      console.log('My items have arrived! ' + itemsList);
    }
    

    If that data is going to be propagated further down your app it is usually suggested to make a call to setState() with your response data from Firebase to trigger a re-render of your components with the new data you just fetched.

    So something along the lines of:

    userRef.once("value", (snap) => {
      itemsList = snap.val().items_list;
    
      this.setState({
        items: itemsList;
      });
    
    }.bind(this));
    
    0 讨论(0)
提交回复
热议问题