Use Firebase-query to get data from an array of ID

2019-06-13 07:33发布

问题:

I'm trying to use Polymerfire's firebase-query to retrieve a list of children for each ID in an array. I have and array with three ID's and a firebase-query that looks like:

<firebase-query id="teamContacts"
                path= "{{teamConPath}}"
                data="{{teamConData}}"></firebase-query>

and a couple observers that look like:

teamConData: {
  observer: '_teamConDataChanged'
},
teamConPath: {
  value: '',
  observer: '_teamConPathChanged'
},

_teamConPathChanged: function(newValue, oldValue) {
  var self = this;
  if (!app.isEmpty(newValue)) {
    this.$.teamContacts.getStoredValue(newValue).then((response) => {
      // self.teamConData = value;
      console.log('in the gsv then');
    });
    console.log('teamConData: ', this.teamConData);
  }
},
_teamConDataChanged: function(newValue, oldValue) {
  console.log('teamConChanged', newValue);
},

Initially the path to teamContacts is null. After I set the path I don't understand how to get and work with the data. The Firebase documentation shows a method called getStoredValue() but that method doesn't seem to be present in the firebase-query source on Github. I thought setting a new path would fetch the data and trigger the observer for the data but that doesn't seem to be the case. How can I get the firebase-query to re-query with the new path and then asynchronously work with the data?

回答1:

I don't know firebase at all, but a quick look at the docs implies it returns a promise of getting the value. So at the point where you console.log teamConData it hasn't yet resolved (ie hasn't fetched the data). Where you added the comment //self.teamConData is where the query should have populated the teamData. As you are using ES6 arrow functions, the this value should be correct - so this.teamConData should have the data in it at that point.



回答2:

As teamConData it's an object I will create an observer like

observers: ['_teamConDataObserver(teamConData.*)'],
_teamConDataObserver: function(changedData) {
    // do something with the data
}

I will recommend you to look at this polycast, it could be helpful https://youtu.be/m1hFhDYDY6o?t=4m35s