0

I need to return the value of boby to another controller but there it arrives as undefined

async functionA({InformationA,informationB}){
 this.generatepayment(InformationA,informationB, function(body,err) {
 console.log(body)  //get the value correctly
 return body;  //but it does not return the value to another controller
})
}

In this function there is an internet service which gives me the value that I want to return in the a function, I need it to be returned from the first function

async generatepayment(InformationA,informationB, callback){
const datasend= {
InformationA: InformationA[0],
InformationB InformacionB
}

const options = {
url: 'https://www',
json: true,
body: datasend
};

request.post(options, (err,res,body) => {
if (err) {
return console.log(err);
}
return callback(body['string_value']);
});
}
Luis
  • 59
  • 6
  • It seems to me that you've made funcitons in which you're performing some asynchronous operation, and trying to return the result of that operation from the callback, which will not work. Have you considered using promises? – Anuj Pancholi Sep 11 '20 at 16:48
  • Yes, the code is trying to use callbacks as if they were promises. See [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Guy Incognito Sep 12 '20 at 06:30

0 Answers0