0

Good afternoon I need help because the designed program shown below manages to transfer the information to the consolelog but it is not able to send it to dialogflow through agent.add function. In mongo db database is the information of people with name, department, position and mail. From dialogflow it will receive the position and the department, and the program must be able to search in the mongodb database a person who fullfils those two requirements and send all the information back to dialogflow. I think the key is to use the find() function and then know how to take that information to an array, but I don't know how. Any help is appreciated. (All functions for connecting to moongose are assumed to be OK.)

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: '+);
         }

`

++ The result coming from the function is an array stored in "res" as coming from mongodb: { _id 6*** department : "Automatic". cargo : "Director" name: "D*** mail: "d***" } The problem is that I don't know how to get from the variable "res" that comes from mongodb, the name and the mail and put it in agent.add to send it to Dialogflow.

++++ A value that is stored in an array (res[0].name) coming from the mongodb database, has to be sent to dialogflow by using agent.add. It turns out that in the visual studio console the value res[0].name appears correctly, but when you add it in agent.add('The name is: '+res[0].name) the result in dialogflow is NOT AVAILABLE. Does anyone know if you need some kind of converter?

  • It seems like your code has error. Could you please try to change```agent.add('The name and email of the person you are looking for is: '+);``` into ```agent.add('The name and email of the person you are looking for is: ');``` . Sorry if you already tried or this is not the your point. – Peter Dec 06 '22 at 03:08
  • Hi, Thank you for your comment. The result coming from the function is an array stored in "res" as coming from mongodb: { _id 6*** department : "Automatic". cargo : "Director" name: "D*** mail: "d***" } The problem is that I don't know how to get from the variable "res" that comes from mongodb, the name and the mail and put it in agent.add to send it to Dialogflow. – Usuario12345 Dec 07 '22 at 09:19

1 Answers1

0

The dialogflow fulfillment library use handler as a promise object.
With your code, the function returns nothing, so it is used as a empty promise.
Handling request is processed before getting DB data.

In order to process handle request AFTER getting data, return promise object and resolve it after adding data to agent.

The query.exec function in mongoose returns promise object, so just return it.
Inside callback function, add response string to agent.
I think this code will work.

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
      );
    });
}
Peter
  • 393
  • 1
  • 3
  • 15
  • Thanks for your answer. When I run the program it returns the variables as undefined. ""The name and email of the person you are looking for is: undefined and undefined. Could it mean that I have to convert the name and mail value coming from mongodb to another format? Thank you. – Usuario12345 Dec 08 '22 at 11:21
  • According to [document](https://mongoosejs.com/docs/api/query.html#query_Query-find) the result is an array. Could you please try this? ```res[0].name + 'and' + res[0].email``` – Peter Dec 08 '22 at 12:57
  • It turns out that when I do that res[0].name stuff in the agent.add function, dialogflow doesn't respond anything, but if I print that value in console.log, I get the value of res[0].name in the console. It must be some conversion problem that dialogflow is not able to read that. Does anyone know something? – Usuario12345 Dec 08 '22 at 16:24