db.findMarami(Scholarship, {}, {scholarship_type:1, _id:0}).then(resultArray => {
let list = resultArray.map(scholarship => scholarship.scholarship_type);
final_list.push(list);
string = final_list.toString();
//console.log(list.toString());
console.log(string);
agent.add(string);
}).catch((error) => { console.log(error) });
This is a code i have for finding data inside a mongodb schema and i would like to use the value from resultArray to add it into the dialogflow chatbot. Inside the promise, the global variable string changes value but after the query, it seems to be empty. When i use the agent.add() function (for displaying text in the chatbot) it does not update the string at all even if the console.log() displays the value in cmd. (DIALOGFLOW MONGODB NODEJS)
function scholarshipTypes(agent){
//static data handling should be shown here
agent.add("There are numerous types of scholarships within DLSU. Listed below are the types of scholarships.");
//mongodb operations to obtain static data from db
db.findMarami(Scholarship, {}, {scholarship_type:1, _id:0}).then(resultArray => {
let list = resultArray.map(scholarship => scholarship.scholarship_type);
final_list.push(list);
string = final_list.toString();
//console.log(list.toString());
console.log(string);
agent.add(string);
}).catch((error) => { console.log(error) });
agent.add("More scholarship details:");
agent.add(new Suggestion("Deadlines"));
agent.add(new Suggestion("Requirements"));
agent.add(new Suggestion("Application Forms"));
}
Above is the full function used by Dialogflow
findMarami: function (model, query, projection, callback) {
return model.find(query, projection);
},
Here is the code to search the db