简体   繁体   English

使用Node.js从对象获取数据

[英]Get data from Object with Node.js

I cant' access data from this Object "snapshots" and I don't know why : 我无法从“快照”对象访问数据,但我不知道为什么:

[
{"action":"Afficher les détails ","cat":"Produits 1","image":"https://www.google.fr","lien":"https://www.google.fr","nom_intent":"1","reponse":"réponse 1","scat":"scat1"},
{"action":"Afficher les détails ","cat":"Produits 2","image":"https://www.google.fr","lien":"https://www.google.fr","nom_intent":"2","reponse":"réponse 2","scat":"scat2"},
{"action":"Afficher les détails ","cat":"Produits 3","image":"https://www.google.fr","lien":"https://www.google.fr","nom_intent":"3","reponse":"réponse 3","scat":"scat3"}
]

I get this object with this code : 我用下面的代码得到这个对象:

function getFirebaseData(endpoint){
      return 
      firebase.database().ref('intent/'+endpoint).once("value", 
      function(snapshot){
            return snapshot.val();
      });
    }

Promise.all([
  getFirebaseData(entities.intent[0].value), 
  getFirebaseData(entities.intent[1].value), 
  getFirebaseData(entities.intent[2].value)
]).then(function(snapshots) {

});

and tried to access like this : 并尝试这样访问:

var action0 = snapshots[0].action;

but action0 is undefined 但是action0undefined

I found a trick to get this thing done ! 我找到了把这件事做好的把戏!
I delcared an array : var firebaseData = []; 我研究了一个数组: var firebaseData = []; firebaseData var firebaseData = [];
then I pushed in it all my values : 然后我把所有的价值观都推了进去:

function getFirebaseData(endpoint){
          return firebase.database().ref('intent/'+endpoint).once("value", function(snapshot){
                firebaseData.push(snapshot.val());
          });
        }

And when the Promises are done, I access to this like that : 当承诺完成后,我将这样访问:

Promise.all([
    getFirebaseData(entities.intent[0].value), 
    getFirebaseData(entities.intent[1].value), 
    getFirebaseData(entities.intent[2].value)])
.then(function(snapshots) {
         var zero = firebaseData[0];
         var first = firebaseData[1];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM