I have a function that runs a GET request from a remote server and brings back the data. I can see the data on the console when I log it but I can't seem to get it as a return value.
async function getUserDetails(telephone){
var full_name = '';
https.get('https://this.server.com/public?telephone', (resp) => {
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
JSON.parse(data, function (key, value) {
if (key == "12") {
full_name+=value;
}});
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log("FULL NAME "+full_name);
});
// An error occurred. Print out the message
}).on("error", (err) => {
console.log("Error: " + err.message);
});
return full_name;
}
I tried setting the data value to a User MODEL on the on('data') but when I try to retrieve the value it returns UNDEFINED.