首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >agent.add()不能工作,尽管console.log()是

agent.add()不能工作,尽管console.log()是
EN

Stack Overflow用户
提问于 2020-04-17 01:05:13
回答 1查看 272关注 0票数 2

我试图实现一个机器人使用电报,对话流和消防基地。我在这个功能上有问题:

代码语言:javascript
复制
function findDoc(agent){
    const userId = request.body.originalDetectIntentRequest.payload.data.from.id.toString();
    const first_name = request.body.originalDetectIntentRequest.payload.data.from.first_name;
    console.log(`Telegram user ID: ${userId}, first_name: ${first_name}`);//THIS SHOWS
    agent.add(`voy a ver si existe el documento`);//THIS SHOWS
    agent.setContext({name: "firstTimer", lifespan:10});
    return db.collection('users').doc(''+userId).get()
      .then((doc) => {
        if (!doc.exists) {
          console.log(`New user created in database `);//THIS SHOWS
          agent.add(`New user created in database`);//THIS DOESN'T SHOW
          var data={
            'id':userId, 
            'name':first_name, 
            'contadorP': 0,
            'doneQuestions': [],
          };
          return db.runTransaction((dataDB)=>{
            dataDB.set(db.collection('users').doc(''+userId), data,{merge:true});
            return Promise.resolve();
          }).catch((err) => {
                console.error(`Error creating file: `+err);
            });
        } else {
          console.log('Found Telegram profile: ', JSON.stringify(doc.data()));//THIS SHOWS
          const data = doc.data();
          agent.add(`User ${data.id} has the name of ${data.nombre}`);//THIS DOESN'T SHOW
        }
      })
      .catch((err) => {
        console.error(err);
      });
  }

我确信这个函数工作得很好,因为firebase控制台中的console.log()工作得很好,因为它们显示了它们应该做的事情;而且我也没有收到任何错误。

下面是我与package.json的依赖关系:

代码语言:javascript
复制
"dependencies": {
    "actions-on-google": "^2.5.0",
    "firebase-admin": "^8.2.0",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.6.1"
  }

我就是这样处理意图的:

代码语言:javascript
复制
  intentMap.set('Default Welcome Intent', welcome);

这是调用findDoc()函数的函数:

代码语言:javascript
复制
function welcome(agent){
    console.log(`Estoy en la funcion de welcome`);
    agent.add(`Welcome`);
    findDoc(agent);
  }

欢迎函数的console.log()和agent.add()都显示了。从我在网上读到的内容来看,依赖关系是很好的,如果是work.So,我不知道还应该尝试什么,因为我(我认为)正确地完成了每个建议-- found.Please帮助.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-25 21:09:47

问题是,尽管findDoc(agent)异步处理并返回承诺,但您并没有将此承诺用作welcome()处理程序的一部分。如果您正在执行任何异步操作,则对话框流实现库要求您从处理程序返回承诺。

由于承诺已经完成,所以对console.log()的调用确实能像预期的那样工作。但是,由于您没有将这一承诺返回给dispatcher,所以它只会将从agent.add()到异步操作的所有内容发回。

在您的例子中,解决方案很简单。由于findDoc()正在返回一个承诺,所以您所需要做的就是让welcome()也返回这个承诺。像这样的事情应该有效:

代码语言:javascript
复制
function welcome(agent){
  console.log(`Estoy en la funcion de welcome`);
  agent.add(`Welcome`);
  return findDoc(agent);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61262516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档