for 循环中的代码不会在行编辑器中的节点 JS 对话流中执行



我是节点js的新手,并使用Dialogflow开发聊天机器人。 为了从 firebase 获得特定的响应,我必须调用如下函数。

admin.database().ref(institutes+'/'+ Programme).once("value").then((snapshot) => {
reply= snapshot.val();}).then((reply) =>                                        {
for (var key in reply)
{
console.log('1 executed');
admin.database().ref(institutes+'/'+ Programme +'/' + key + '/' + 'Group').once("value").then((datagroup)=>
{
if(datagroup.val() == Group)
{
console.log('2 executed');
result+=key;
}
});
}
});
agent.add(`Hello`);

代码成功运行,回复中的 hello 消息,但它没有执行写在 for 循环中的函数。 即在控制台中没有像 1 执行或 2 执行这样的消息。

我建议你使用此示例代码来构建你的代码:

function readFromDb (agent) {
// Get the database collection 'dialogflow' and document 'agent'
const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');
// Get the value of 'entry' in the document and send it to the user
return dialogflowAgentDoc.get().then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add(doc.data().entry);
}
return Promise.resolve('Read complete');
}).catch(() => {
agent.add('Error reading entry from the Firestore database.');
agent.add('Please add a entry to the database first by saying, "Write <your phrase> to the database"');
});
}

dialogflow/fulfillment-firestore-nodejs存储库中的此 github 文件中获取的代码

我用它来制作我的节点.js代码,以便在 Firebase 上部署。我不在其中使用 for,但在 mi 的情况下,我需要完美地使用 if 和这项工作。

最新更新