【问题标题】:How can I transfer the information from mongodb database to dialogflow with node.js?如何使用 node.js 将信息从 mongodb 数据库传输到 dialogflow?
【发布时间】:2022-12-12 12:43:55
【问题描述】:

下午好 我需要帮助,因为下面显示的设计程序设法将信息传输到控制台日志,但无法通过 agent.add 函数将其发送到 dialogflow。 mongo db 数据库中是人员的姓名、部门、​​职位和邮件等信息。它将从 dialogflow 接收位置和部门,并且程序必须能够在 mongodb 数据库中搜索满足这两个要求的人并将所有信息发送回 dialogflow。我认为关键是使用 find() 函数,然后知道如何将该信息放入数组,但我不知道如何操作。任何帮助表示赞赏。 (假设连接到 moongose 的所有功能都正常。)

function ConsultarDepartament(agent) {

        var departament = agent.parameters.departamentos;
        var cargo = agent.parameters.cargodepartamento;
  
       contactodepartamento.find({departament : 'departament', cargo : 'cargo'}) 
                 .exec((err, res)=>{
           if(err) return console.log('Error ' + err)
              else console.log(res);

                   });
          agent.add('The name and email of the person you are looking for is: '+);
         }

`

++ 来自函数的结果是一个存储在“res”中的数组,来自 mongodb: { _id 6*** 部门:“自动”。 货物:“导演” 名称:“D*** 邮件:“d***” } 问题是我不知道如何从来自 mongodb 的变量“res”中获取名称和邮件并将其放入 agent.add 以将其发送到 Dialogflow。

++++ 存储在来自 mongodb 数据库的数组 (res[0].name) 中的值必须使用 agent.add 发送到 dialogflow。事实证明,在 visual studio 控制台中,值 res[0].name 显示正确,但是当您将其添加到 agent.add('The name is: '+res[0].name) 时,dialogflow 中的结果不是可用的。有谁知道你是否需要某种转换器?

【问题讨论】:

  • 看起来你的代码有错误。您能否尝试将agent.add('The name and email of the person you are looking for is: '+); 更改为agent.add('The name and email of the person you are looking for is: ');。抱歉,如果您已经尝试过,或者这不是您的重点。
  • 您好,感谢您的评论。来自该函数的结果是一个存储在“res”中的数组,来自 mongodb:{ _id 6*** department:“Automatic”。 cargo : "Director" name: "D*** mail: "d***" } 问题是我不知道如何从来自 mongodb 的变量 "res" 中获取名称和邮件以及将其放入 agent.add 以将其发送到 Dialogflow。

标签: node.js mongodb promise dialogflow-es dialogflow-es-fulfillment


【解决方案1】:

dialogflow fulfillment library 使用处理程序作为承诺对象。
使用您的代码,该函数不返回任何内容,因此它被用作空承诺。
在获取数据库数据之前处理处理请求。

为了在获取数据后处理处理请求,返回承诺对象并在将数据添加到代理后解析它。

mongoose中的query.exec函数返回的是promise对象,直接返回即可。
在回调函数中,将响应字符串添加到代理。
我认为这段代码会起作用。

function ConsultarDepartament(agent) {
  var departament = agent.parameters.departamentos;
  var cargo = agent.parameters.cargodepartamento;

  return contactodepartamento
    .find({departament : 'departament', cargo : 'cargo'}) 
    .exec((err, res)=>{
      if(err) return console.log('Error ' + err);

      agent.add('The name and email of the person you are looking for is: '+ 
        res.name + 'and' + res.email
      );
    });
}

【讨论】:

  • 感谢您的回答。当我运行程序时,它返回未定义的变量。 ""您要找的人的姓名和电子邮件是:undefined and undefined。这是否意味着我必须将来自 mongodb 的名称和邮件值转换为另一种格式?谢谢你。
  • 根据document,结果是一个数组。你能试试这个吗? res[0].name + 'and' + res[0].email
  • 事实证明,当我在 agent.add 函数中执行 res[0].name 操作时,dialogflow 不响应任何内容,但如果我在 console.log 中打印该值,我将获得 res[0] 的值。控制台中的名称。 dialogflow 无法读取它一定是一些转换问题。有人知道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
相关资源
最近更新 更多