0

I am trying to loop through JSON using Node so that I can call a stored procedure in a sql database. the JSON is:

[ { boardid: '1', accesid: '2' },
  { boardid: '2', accesid: '3' },
  { boardid: '8', accesid: '4' } ]

the pseudo code i want to implement is: (I have the UserID)

var data = req.body.addJSON

for each JSON object {
    con.query(
        "CALL addUserToBoard('" + UserID + "', '" + BoardID + "','" + AccessTypeID + "');",
        function(err, result, fields) {
            if (err) throw err;
        }
    );
}
1
  • 1
    What is it you are asking? Please tell us what issues you are running into and what you have tried so far Commented Feb 8, 2018 at 15:33

2 Answers 2

1

You can always interate in an Object a follows

var jsonresponse = JSON.parse(data);
Object.keys(jsonresponse).forEach( function(param , index) {
    console.log(jsonresponse[param]);
    console.log(index);
}); 
Sign up to request clarification or add additional context in comments.

Comments

0

You could do this using a simple forEach,

var data = req.body.addJSON


data.forEach(value => {
    con.query("CALL addUserToBoard('" + UserID + "', '" + value.boardid + "','" + value.accesid + "');", function (err, result, fields) 
    {
        if (err) throw err;
    }); 
})

1 Comment

No problem. Please accept the answer as correct if it works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.