1

I'm using Node.js with Express for a back-end server. I am sending in some data as a json and after parsing it I'm trying to iterate through the object so I can assign values. For a test I'm passing in an object that has 50 objects in it.

I've tried using for in loop with hasOwnProperty but it has never completed all of them.

for (i in req.body.deviceObject) {
    if (req.body.deviceObject.hasOwnProperty.call(req.body.deviceObject[i].dId, i)) {
      newVcsObject[i] = new PQ(
        'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)'
      );
      newVcsObject[i].values = [
        req.body.deviceObject[i].ipAddress,
        req.body.customerName,
        req.body.deviceObject[i].uName,
        req.body.deviceObject[i].uPassword,
        req.body.deviceObject[i].VCSID
      ];
      console.log(i);
      count += 1;
    }
}

edit: this is my data structure:
"deviceObject": {
        "1": {
            "rId": "e43aebb5-234f-4aa6-a666-90179df767bc",
            "e164": "449bc7cc-90fa-4b9e-b4c1-1223d825d545",
            "uName": "Server",
            "uPassword": "admin",
            "VCSID": "54191576-47ea-4055-8ea4-bc201dc54f6d",
            "ipAddress": "1.1.1.1",
            "dId": "b6178041-86cc-4959-9155-54ca419083e7"
        },
        //there are more in between, this is where it's stuck
        "35": {
            "rId": "dce82b00-fa1e-46b8-a3f6-1a5af45175de",
            "e164": "7cc8190b-c261-40f8-9f62-408f7e8b2450",
            "uName": "access point",
            "uPassword": "admin",
            "VCSID": "3e3e447c-b9fe-4997-ba54-225175b0a84b",
            "ipAddress": "1.1.1.1",
            "dId": "9c97d5a5-b26f-492e-ba5b-bdd33eb3cb30"
        }
        // goes all the way to 50 

I always go through 35/50 of the input. I have double checked and the server is receiving all 50 of them without a problem, but it only iterates through the first 35.

abeldattsun
  • 61
  • 1
  • 7
  • 1
    Could you attach the data structure your looping over? – kSp Aug 27 '19 at 13:47
  • Are you trying to loop over properties on the object? If so look at this https://stackoverflow.com/questions/8312459/iterate-through-object-properties – TemporaryFix Aug 27 '19 at 13:47
  • Is something different about the 35th object? Try echoing out deviceObject that doesn't match the if-statement. – Gavin Aug 27 '19 at 13:47
  • 1
    For loops typically have no reason to not iterate through all objects. Is the application hanging during iteration? Also your count +=1 is within an if statement, so it does not guarantee that the count will be incremented. Add another variable (totalcount) to increment outside the if statement within the for loop to check if all items were iterated. – GLJ Aug 27 '19 at 13:48
  • @kSp I edited the original so it goes through. – abeldattsun Aug 27 '19 at 13:51
  • @Programmatic not entirely what I'm trying to do, but close! – abeldattsun Aug 27 '19 at 13:51
  • @Gavin it's all generated in the front end and they're all identical except the unique ID's – abeldattsun Aug 27 '19 at 13:51
  • @GLJ The count only supposed to count every time the newVcsObject has been added so it is in the correct spot. – abeldattsun Aug 27 '19 at 13:51
  • if looks strange - do not think you need hasOwnProperty here and is dId in all records ? I would ask ... .hasOwnProperty(i) in case probably... – Jan Aug 27 '19 at 14:49
  • @Tom the reason dId is not pushed to the database here is because it is pushed into another table. I just use it to make sure that it is there. I can try to exclude hasOwnProperty and see if it works. – abeldattsun Aug 27 '19 at 14:51
  • @Tom other then ES-lint complaining it actually does all 50 this way .Can you please explain why? – abeldattsun Aug 27 '19 at 14:52
  • Try to compare object's content or put them here or to https://pastebin.com/ etc. – Jan Aug 27 '19 at 15:01

2 Answers2

0

const deviceObject = {
  1: {
    rId: 'e43aebb5-234f-4aa6-a666-90179df767bc',
    e164: '449bc7cc-90fa-4b9e-b4c1-1223d825d545',
    uName: 'Server',
    uPassword: 'admin',
    VCSID: '54191576-47ea-4055-8ea4-bc201dc54f6d',
    ipAddress: '1.1.1.1',
    dId: 'b6178041-86cc-4959-9155-54ca419083e7',
  },
  35: {
    rId: 'dce82b00-fa1e-46b8-a3f6-1a5af45175de',
    e164: '7cc8190b-c261-40f8-9f62-408f7e8b2450',
    uName: 'access point',
    uPassword: 'admin',
    VCSID: '3e3e447c-b9fe-4997-ba54-225175b0a84b',
    ipAddress: '1.1.1.1',
    dId: '9c97d5a5-b26f-492e-ba5b-bdd33eb3cb30',
  },
};
const custName = 'dummy'; // req.body.customerName;
const array = [];

Object.keys(deviceObject).forEach((r) => {
  const text = 'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)';
  const vals = deviceObject[r];
  const values = [vals.ipAddress, custName, vals.uName, vals.uPassword, vals.VCSID];
  const obj = {
    text,
    values,
  };
  array.push(obj);
});

console.log(array);


 
Neel Rathod
  • 2,013
  • 12
  • 28
0

So this isn't quite an answer, but I'm hoping this will aid in your debugging.

If your for loop has terminated, than that means it has successfully iterated through every object. If you are wondering why a new VcsObject was not correctly parsed, than it appears that the if statement did not succeed. Try using an else statement with a log to see if any failure occurred in you if statement so all control logic is accounted.

for (i in req.body.deviceObject) {
    if (req.body.deviceObject.hasOwnProperty.call(req.body.deviceObject[i].dId, i)) {
      newVcsObject[i] = new PQ(
        'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)'
      );
      newVcsObject[i].values = [
        req.body.deviceObject[i].ipAddress,
        req.body.customerName,
        req.body.deviceObject[i].uName,
        req.body.deviceObject[i].uPassword,
        req.body.deviceObject[i].VCSID
      ];
      console.log(i);
      count += 1;
    } else {
        // Log when the if-statement fails to execute
        console.log("Warning! If statement failed on iteration " + i)
    }
}

If your for loop has never terminated and is hung up, than that means you have some function that never returned. I'm not sure if this is your case, but then you'll need to either make several log statements to see at which point it failed or know which function may have never returned.

for (i in req.body.deviceObject) {
    console.log("Iteration: " + i)
    console.log("checking hasOwnProperty")
    if (req.body.deviceObject.hasOwnProperty.call(req.body.deviceObject[i].dId, i)) {
      console.log("Assigning a new PQ object")
      newVcsObject[i] = new PQ(
        'insert into VCS(ip_address, vcs_name, user_name, user_password, ID) values ($1, $2, $3, $4, $5)'
      );
      console.log("Assigning newVcsObject values")
      newVcsObject[i].values = [
        req.body.deviceObject[i].ipAddress,
        req.body.customerName,
        req.body.deviceObject[i].uName,
        req.body.deviceObject[i].uPassword,
        req.body.deviceObject[i].VCSID
      ];
      console.log(i);
      count += 1;
    } else {
        // Log when the if-statement fails to execute
        console.log("Warning! If statement failed on iteration " + i)
    }
}
GLJ
  • 1,074
  • 1
  • 9
  • 17
  • Thanks! that's a good idea for debugging! The issue came from using hasownproperty. It stopped after a while. Not sure why. I disabled it and after testing it, it seems to be fixed – abeldattsun Aug 27 '19 at 19:00
  • No problem! For debugging, start which big chunks of logic and slowly become more granular as you pinpoint the problem. As in my example, first I checked to see whether or not the if statement was executing. After that, I decided to check even further by adding the print statement after each line of execution to see if the results returned. The next step after that would be to begin diving within each called function to ensure no unknown side effects took place. – GLJ Aug 27 '19 at 20:17
  • Also @abeldattsun Since the actual code that is breaking is not provided, I suggest placing the faltering code within your OP or closing this question as resolved. – GLJ Aug 27 '19 at 20:59
  • the code that breaks is right there. That is where the fault is. I just removed the hasOwnProperty and that fixed the issue – abeldattsun Aug 28 '19 at 12:12