簡體   English   中英

使用Node.js從對象獲取數據

[英]Get data from Object with Node.js

我無法從“快照”對象訪問數據,但我不知道為什么:

[
{"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"}
]

我用下面的代碼得到這個對象:

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) {

});

並嘗試這樣訪問:

var action0 = snapshots[0].action;

但是action0undefined

我找到了把這件事做好的把戲!
我研究了一個數組: var firebaseData = []; firebaseData var firebaseData = [];
然后我把所有的價值觀都推了進去:

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

當承諾完成后,我將這樣訪問:

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