Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript agent.add()不工作,但console.log()不工作_Javascript_Firebase_Google Cloud Firestore_Dialogflow Es_Telegram Bot - Fatal编程技术网

Javascript agent.add()不工作,但console.log()不工作

Javascript agent.add()不工作,但console.log()不工作,javascript,firebase,google-cloud-firestore,dialogflow-es,telegram-bot,Javascript,Firebase,Google Cloud Firestore,Dialogflow Es,Telegram Bot,我正在尝试使用电报、Dialogflow和Firebase实现一个机器人。 我在使用此功能时遇到问题: function findDoc(agent){ const userId = request.body.originalDetectIntentRequest.payload.data.from.id.toString(); const first_name = request.body.originalDetectIntentRequest.payload.data.fro

我正在尝试使用电报、Dialogflow和Firebase实现一个机器人。 我在使用此功能时遇到问题:

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);
      });
  }
function welcome(agent){
    console.log(`Estoy en la funcion de welcome`);
    agent.add(`Welcome`);
    findDoc(agent);
  }
我确信这个函数工作得很好,因为firebase控制台中的console.log()工作得很好,因为它们显示了它们应该做的事情;我也没有收到任何错误回复

以下是my package.json中的依赖项:

"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"
  }
这就是我处理意图的方式:

  intentMap.set('Default Welcome Intent', welcome);
这是调用findDoc()函数的函数:

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);
      });
  }
function welcome(agent){
    console.log(`Estoy en la funcion de welcome`);
    agent.add(`Welcome`);
    findDoc(agent);
  }
显示欢迎函数的console.log()和agent.add()。
从我在网上读到的内容来看,依赖关系很好,应该可以工作。所以我不知道还有什么可以尝试,因为我(我认为)已经正确地完成了找到的每个建议。请帮助…

问题是,尽管
findDoc(agent)
异步处理事情并返回承诺,您没有将此承诺用作
welcome()
处理程序的一部分。dialogflow实现库要求您在执行任何异步操作时从处理程序返回承诺

它似乎可以工作,因为承诺正在完成,所以对
console.log()
的调用可以按预期工作。但是,由于您没有将此承诺返回给dispatcher,因此它只返回从
agent.add()
到异步操作点的所有内容

在您的情况下,解决方案很简单。因为
findDoc()
正在返回一个承诺,所以您需要做的就是让
welcome()
也返回这个承诺。像这样的方法应该会奏效:

function welcome(agent){
  console.log(`Estoy en la funcion de welcome`);
  agent.add(`Welcome`);
  return findDoc(agent);
}

您的示例显示显式调用
return findDoc(agent)
。能否更新问题以显示正在进行此调用的函数?如果您更新代码以显示如何为正在调用的意图注册意图处理程序,这也会有所帮助。您好,我将代码更新为最新版本…仍然存在相同的问题。很抱歉花了这么长时间回答…非常感谢!!!我不敢相信我之前还没有弄明白……你救了我的最后一个学位项目;希望我不需要更多的帮助。非常感谢。有时候很容易被承诺绊倒,这就是为什么我问你如何称呼它的细节。很高兴它起到了作用——如果你确实需要进一步的帮助,请记住创建一个新的问题,尽可能详细。我的情况与Eugenia相同,但在你的解决方案@Capture之后仍然有困难