1
I am creating a module with Nodejs and in this module I created a function that returns a value, being this value updated within a function of type callback. The problem is that this variable/value is not being updated. I would like to know how to solve the problem. I’m a beginner in Nodejs and I researched it, but I couldn’t find the solution to my problem. Below the code, the variable I refer to is "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;
}
I understand what you mean. But is there any way that I could return this exit to another place? because I am using the sendMessage function in another/script file, there I just make the function call and wait for the return to process the answer. If I process the answer with another function like you did in callback’s 'Else', is there a way to return that answer to that other file I’m using? I thank you in advance for your patience.
– CloudAC
You will need a function there in the other file that is visible from that, similar to what I did in the reply, but there in the other file. Or you can do the function
sendMessage
receive as parameter the function itself to be called. I edited the answer with this solution.– Pablo Almeida
I get it. I’ll try to do that and see if it solves my problem. Thank you very much for answering and for your patience. I am a complete beginner in Node and need to do a project using this tool. I ended up learning a lot from your answer. Thank you very much, I will mark it as correct!
– CloudAC