-2

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);
Dolcekar
  • 39
  • 1
  • 10
  • Yes it is the same.... You are trying to read an asynchronous request before it returns. You order a pizza and you are trying to eat the pizza it as soon as you make the call. You are not waiting for the pizza to be made and delivered. The place where you have the for loop is where the pizza is delivered. That is where you can eat the pizza. Your logic needs to be in there or you need to rethink how it works. – epascarello Jul 25 '19 at 20:21
  • of course, I wouldn't be here if I didn't want to learn... let's just not make assumptions. That's all. Let's not assume it's the same answer nor assume I don't want to learn. I am working with many others things, not just JavaScript, not even just coding. I wish I knew it all, but chances are in 3 years I may need to refer back to some of my very own problems and relearn a couple things. Thank you though <3 – Dolcekar Jul 25 '19 at 20:50

1 Answers1

-2

Since request will execute in asynchronous way, it will not wait for request to complete so it will execute the next line before we get response from request hence it is undefined.

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]; };

 r["extractedData"] = extractedData;

   console.log(extractedData);
 }); 

// this will execute before request gets complete hence it is undefined
 console.log(r.extractedData);

So create promise

Getdata() {
return new promise ((resolve, reject) =>{
   request(options, function (error, response, xyz) { 
    var deviceData = JSON.parse(xyz); 
    if (error) reject (error) ;
    var extractedData = []; 
    for (var i = 0; deviceData.data.length > i; i++)
{ 
 extractedData.push(deviceData.data[i]);
 };   
       resolve (extractedData) 
     }); 
}
}

Call this function

Var r ={}
Getdata ().then(data=>{
 r["extractedData"] = data;
},err=>console.log(err)) 

This will work for your problem

ajaykumar mp
  • 487
  • 5
  • 12
  • ajaykumar I still get "undefined" exactly by doing what you did – Dolcekar Jul 25 '19 at 17:45
  • @Dolcekar check the edited answer... It might help you. – ajaykumar mp Jul 26 '19 at 02:21
  • thank you so much for the update! I tested this and I get "ReferenceError: promise is not defined" I have read some forums where this could be a Node.js library conflict, but I'll keep digging in it. If you think of the solution, I'll appreciate your input, so far you've been the only one who's contributed some logical approach to my problem – Dolcekar Jul 26 '19 at 12:45
  • promise should be capital Promise p should be capital.It will work. Please post your code here so that i can take a look at it.tnx – ajaykumar mp Jul 26 '19 at 13:00
  • I just updated my code, you can see what my error messages prompted. Big thanks! I am hoping this can work today so I can move on with the front-end. It'll be all thanks to people like you <3 – Dolcekar Jul 26 '19 at 13:49
  • Please console.log both xyz and reposnse so that i can see the json format – ajaykumar mp Jul 26 '19 at 13:59
  • @Dolcekar There is nothing wrong in the code, check your response, usually data will be available in the reponse object not in the xyz, if so then check the properties of that object. Happy coding – ajaykumar mp Jul 26 '19 at 15:01
  • ajaykumar, I accessed the data through the response, xyz was my API data, but Node.js stores it in a "body" variable. Either way I can't access it. I think the problem is timing. The request is not complete by the time I try to manipulate it. I have to use setTimeout or some scheduling function. Anyway, thanks for the help. Alternatively I can just store everything in a static .json file and work from there. Thanks!! – Dolcekar Jul 26 '19 at 18:11
  • by the way, I forgot to update this. I figured it out, I used an async function and "await" for the response. It's working properly now! – Dolcekar Jul 31 '19 at 13:20