Javascript 如何处理对Firestore和call agent.add()的承诺调用
我正在尝试从DialogFlow内联编辑器调用Cloud Firestore Beta,然后调用agent.add(…),但我不知道需要做什么才能使它正常工作。调用本身工作得很好,我得到了正确的值,但是执行仍在继续,即使返回后承诺也不会继续执行agent.add()部分。这是我的密码:Javascript 如何处理对Firestore和call agent.add()的承诺调用,javascript,promise,chatbot,Javascript,Promise,Chatbot,我正在尝试从DialogFlow内联编辑器调用Cloud Firestore Beta,然后调用agent.add(…),但我不知道需要做什么才能使它正常工作。调用本身工作得很好,我得到了正确的值,但是执行仍在继续,即使返回后承诺也不会继续执行agent.add()部分。这是我的密码: function foodcatHandler(agent){ if (request.body.queryResult.allRequiredParamsPresent === true) { va
function foodcatHandler(agent){
if (request.body.queryResult.allRequiredParamsPresent === true) {
var foodIDsRef = firestore.collection('foodcats').doc('foodIDs');
var category = request.body.queryResult.parameters['category-codes'];
console.log(category);
var catVal;
foodIDsRef.get().then( doc => {
console.log("Promise returned");
catVal = doc.get(category);
agent.add("Sounds like a plan");
agent.add("Here are all the restaurants serving " + category + " in your area.");
agent.add(new Card({
title: `Foody`,
text: `The easiest way to order is now even simpler!!`,
buttonText: 'Order Now!',
buttonUrl: 'https://beta.foody.com.cy/' + pcode + '?categories=' + catVal
})
);
});
}
}
不起作用的部分是agent.add()语句。控制台输出有时工作,有时不工作。有人能帮我解决这个问题吗
在网上研究了一下之后,我找到了以下解决方案:
return foodIDsRef.get().then( doc => ...
显然,您需要返回承诺,这将允许代理继续对话。解决方案是您需要返回承诺。这就是调用DB的时候,只需使用return关键字。尝试向承诺链添加一个
.catch(err=>{console.log(err);throw err;})
以使任何错误都可以被观察到。您有没有收到任何错误?我没有收到任何错误没有