Update:
The problem below is trying to persist data using an asynchronous call... very inefficient and unreliable. I looked into socket.io but it seems to be ideal for messaging systems. Can any one advise of a better solution/library? The problem is, suppose I am trying to make a connection with a device, that device is streaming data every .8 sec. The data comes as a JSON object, with values timestamped. If I wanted to chart these values and create a seemingly real-time feel, what should I be considering? What I came across in my research: socket.io rabitQt MQTT
Old solution below, asynchronous call is a mess, it has nothing to do with knowing JavaScript callback functions or not, I believe this is just not the way.This is why this is not quite a duplicate question to "How do I return the response from an asynchronous call? " it is not a simple callback or promise.
I'm trying to access my JSON object, so I can map out the data and manipulate it. However, I can't seem to be able to access it outside of my request function. I put my request function inside a variable and tried to call it by calling the parent's properties, but in my command line I read "undefined" what am I doing wrong?
1) I tried using the push() method inside my request function, so I could create some prototypical object that stores all the data, then contain it within a function so I could use it outside my request, but I get the error ".push() is not a function"
var r = request(options, function (error, response, xyz) {
var deviceData = JSON.parse(xyz);
if (error) throw new Error(error);
for (var i = 0; deviceData.data.length > i; i++) {
var extractedData = [];
extractedData = deviceData.data[i];
};
//this executes an entire JSON object with all the data I need
//console.log(extractedData);
});
//my console shows "undefined"
console.log(r.extractedData);
I expect to get a whole JSON object like I do when I console.log inside the request function. Instead, I get "undefined"
UPDATED I tried using a promise as @ajaykumar has suggested, I ran into the issues described
var xyz = {};
var r = {};
Getdata().then(data => {
r["extractedData"] = data;
//console shows all the data as inside the promise block
//console.log(data)
}, err => console.log(err));
function Getdata() {
return new Promise((resolve, reject) => {
request(options, function (error, response) {
var deviceData = JSON.parse(response.body);
//console.log(xyz) at this point shows all data, message: call successful" and the data looks like this:
[console.log(xyz)][1]
//response shows the entire object too, but it is through Node.js, so you actually see my object contained by the "body" property of Node.js. I didn't think about this before. So now I modified my code and removed xyz as it is actually not necessary
if (error) reject(error);
var extractedData = [];
for (var i = 0; deviceData.data.length > i; i++) {
extractedData.push(deviceData.data[i]);
};
//the console shows all my data
//console.log(extractedData)
resolve(extractedData);
});
//the console no longer associates extractedData, displays "extractedData is undefined"
//console.log(extractedData)
});
}
//console prompts error "ReferenceError: data is not defined", if I try extractedData instead it gives me the same old message "undefined". If I try r[0] I get "undefined"
//console.log(data);