dimanche 1 février 2015

My for loop is breaking my code


Here's the for loop in question:



console.log(sender1[0]); // returns user object
console.log(sender1.length); // returns 1
for (var i = 0; i < sender1.length; i = i + 1) {
console.log(sender1[i].phone); // returns can't read phone property of undefined
}


Here's where I'm getting sender 1



User.find().where({
code: req.body.Body.toUpperCase()
}).exec(function (err, sender1) {


This is using mongoose, so sender1 is already an array. I tried using .toArray and it said find has no method "toArray".


If I replace all the sender[i] with sender[0], everything works fine. But it's saying sender[i].somename is undefined. What's wrong with my loop?





Aucun commentaire:

Enregistrer un commentaire