【发布时间】: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