I am creating a module with NodeJS and in this module I created a function that returns a value, this value being updated within a callback function. The problem is that this variable / value is not being updated. I would like to know how to solve the problem. I am a beginner in NodeJS and I researched it, but I did not find the solution to my problem. Here is the code, the variable I mean is the "output", declared at the beginning of the function:
exports.sendMessage = function (chat, message, workspaceId){
let output = "";
chat.message(
{
input: { text: message },
workspace_id: workspaceId
},
function (error, response){
if(error){
console.error(error);
} else {
output = response;
console.log(JSON.stringify(response, null, 2));
}
}
);
return output;
}