【问题标题】:Dialogflow : Getting in an array all document IDs of a firestore collection (javascript node)Dialogflow:获取一个数组中的所有文档 ID 的 firestore 集合(javascript 节点)
【发布时间】:2019-08-23 12:22:51
【问题描述】:

我一直在尝试从我的 firestore 集合中检索所有文档 ID。

例如。如果我的集合中有两个文档,我想获取一个包含集合中所有文档 ID 的数组。 即:var myArray = ['HhYaTvok4qJaIAVI9R0','2AO4RbZ6K5Db4q3OSqi'];

我一直在尝试使用 firestore 文档 (https://github.com/firebase/snippets-node/blob/9ae3a00985b53025fdc82716046882af71b6009d/firestore/main/index.js#L622-L627) 中的一些代码。

但它不会在对话流中检索任何内容

在 index.js(内联编辑器)中我的代码下方。


// The function to get documents ids
const arraymaker = (db) => {
  let platformRef = db.collection('quiz');
    let all_doc = platformRef.get();
    return all_doc
      .then(snapshot => {
        var myArray =[];
          snapshot.forEach(doc => {
            myArray.push(doc.id);   
          });
        })
      .catch(err => {
        console.log('Error getting documents', err);
      });
  };

// database retrieve
 app.intent('Read Random Quizz', (conv) => {
   // Trying to get Data from firestone DB,
    //var myArray = ['HhYaTvok4qJaIAVI9dR0','2AO4RbZ6K5Db4q3OS7qi'];
    var myArray = arraymaker(db);
    var rand = myArray[Math.floor(Math.random() * myArray.length)];
    var platformRef = db.collection("quiz").doc(rand);
    return platformRef.get()
             .then( snap => {
               var dat = "";
               var answ = "";
                 if (snap.exists) {
                   dat =  snap.data().question;
                   answ =  snap.data().answer;
                 }
               // This is the response for Actions on Google
                reply(conv,`The question is ${dat} and the answer is ${answ}`);
            })
            .catch( err => {
              console.log("error...", err);
            });
 });

我认为问题出在:

var myArray = arraymaker(db);

它只是不返回带有所有文档 ID 的 myArray。

当我单独运行该函数时,我可以看到一个 firestore 将所有 id 附加到一个数组中。但只是在 firestore 日志中。它不会返回不在对话流中的最终 myArray。

【问题讨论】:

  • 您已经展示了确实工作的代码。您能否更新您的问题以显示一些没有的代码?仅获取一个文档与获取所有文档或所有列表不同,处理 Intent 中的文档也略有不同。
  • 谢谢囚徒!我刚刚更新了问题
  • 你必须在第二个例子中返回 all_doc,
  • 感谢 Yasser Mas,它帮了很多忙。在返回最终数组时,我仍然遇到一些问题。我相应地编辑了我的问题。

标签: javascript node.js dialogflow-es


【解决方案1】:

你必须在第二个例子中返回 all_doc。 app.intent 必须像第一个示例一样返回一个 Promise,而在第二个示例中您不会返回其中的任何内容。

除了你用 console.log 覆盖 myArray !并为每个文档调用回复,我不熟悉 firestore,但我认为您必须在每个文档之后调用回复并更改覆盖 myArray

var myArray ="";
    snapshot.forEach(doc => {
      myArray += doc.id;           
    });
 reply(conv,`Your list of IDs is ${myArray}`);

【讨论】:

    【解决方案2】:

    我这样做了,这似乎有效。最初,我想要的是检索一个随机文档,所以我粘贴了我找到的解决方案。如前所述,我是 javascript 的菜鸟,这可能是一个非常糟糕的代码。因此,任何关于如何改进这一点的建议都将受到热烈欢迎。

    
    var GlobalVariable;
    
    // The function to get documents ids in an array called GlobalVariable
    const arraymaker = () => {
      let platformRef = db.collection('quiz');
        let all_doc = platformRef.get();
        return all_doc
          .then(snapshot => {
            var myArray =[];
              snapshot.forEach(doc => {
                myArray.push(doc.id);
          GlobalVariable= myArray;       
              });
            })
          .catch(err => {
            console.log('Error getting documents', err);
          });
      };
    
    
    // database retrieve
     app.intent('Read Random Quizz', (conv) => {
        arraymaker(); // restart function to get all the IDs in an array
        // take a random ID and read the document   
        var rand = GlobalVariable[Math.floor(Math.random() * GlobalVariable.length)];
        var platformRef = db.collection("quiz").doc(rand);
        return platformRef.get()
                 .then( snap => {
                   var dat = "";
                   var answ = "";
                     if (snap.exists) {
                       dat =  snap.data().question;
                       answ =  snap.data().answer;
                     }
                   // This is the response for Actions on Google
                    reply(conv,`The question is ${dat} and the answer is ${answ}`);
                })
                .catch( err => {
                  console.log("error...", err);
                });
     });
    

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 2019-02-05
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多