This question already has an answer here:
I am trying to iterate POST request in Node.JS. My request is
{"MAC":"BEEC","Config":[{"ID":"122","ctpt_set":false},{"ID":"123","ctpt_set":true}]}
For POST request My code is below
for(var i=0; i < req.body.Iterate.length;i++){
set[i] = req.body.Iterate[i].set;
console.log(set);
console.log(i);
ConfigSchema.findOne({"address":req.body.Config[i].ID},function(err,Iterate){
if (err)
{
res.status(400).send({ "Error": "Server Error","err":err});
}
if(Iterate)
{
console.log(i);
}
else
{
res.status(400).send({"message":"Invalid Nodes"})
}
});
};
res.status(200).send({ "message": "Hit" });
console.log('here');
Response I am getting is
[ false ]
0
[ false, true ]
1
here
2 //Problem Coming in 2nd Iteration
POST /v/endpoint/status 200 50.649 ms - 17
2
I wanted to get
[ false ]
0
0 //Solution
[ false, true ]
1
1
POST /v/endpoint/status 200 50.649 ms - 17
here
Is problem with If statement or for loop or my find function?
Aucun commentaire:
Enregistrer un commentaire