我的for循环打破了我的代码

时间:2015-02-01 19:59:46

标签: javascript node.js mongoose

这是有问题的for循环:

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
}

这是我收件人1的地方

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

这是使用mongoose,所以sender1已经是一个数组。我尝试使用.toArray,它说find没有方法“toArray”。

如果我用发件人[0]替换所有发件人[i],一切正常。 但是它说发件人[i] .somename是未定义的。我的循环出了什么问题?

第二次更新:

    2015-02-01T20:38:20.900903+00:00 app[web.1]: 
    2015-02-01T20:38:20.930258+00:00 app[web.1]: error: Script restart attempt #1
    2015-02-01T20:38:20.894952+00:00 app[web.1]: this is sender1 length  1
    2015-02-01T20:38:20.895077+00:00 app[web.1]: The for loop executed!
    2015-02-01T20:38:20.895465+00:00 app[web.1]: this is sender1 section ["54baf92d2b54840900dcb971"]
    2015-02-01T20:38:20.895504+00:00 app[web.1]: jj is 0
    2015-02-01T20:38:20.900455+00:00 app[web.1]: This is the from1 phone +19206398297
    2015-02-01T20:38:20.901052+00:00 app[web.1]: console.log('This is the section1 phone',sender1[jj].phone);
    2015-02-01T20:38:20.901050+00:00 app[web.1]: /app/packages/timeoff/server/controllers/timeoff.js:104
    2015-02-01T20:38:20.903068+00:00 app[web.1]: TypeError: Cannot read property 'phone' of undefined
    2015-02-01T20:38:20.901184+00:00 app[web.1]:                                                     ^
    2015-02-01T20:38:20.903071+00:00 app[web.1]:     at Promise.<anonymous> (/app/packages/timeoff/server/controllers/timeoff.js:104:53)
    2015-02-01T20:38:20.903072+00:00 app[web.1]:     at Promise.<anonymous> (/app/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
    2015-02-01T20:38:20.903074+00:00 app[web.1]:     at Promise.emit (events.js:95:17)
    2015-02-01T20:38:20.903075+00:00 app[web.1]:     at Promise.emit (/app/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
    2015-02-01T20:38:20.903077+00:00 app[web.1]:     at Promise.fulfill (/app/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
    2015-02-01T20:38:20.903079+00:00 app[web.1]:     at model.Document.init (/app/node_modules/mongoose/lib/document.js:250:11)
    2015-02-01T20:38:20.903078+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/query.js:1394:13
    2015-02-01T20:38:20.903081+00:00 app[web.1]:     at completeOne (/app/node_modules/mongoose/lib/query.js:1392:10)
    2015-02-01T20:38:20.903082+00:00 app[web.1]:     at Object.cb (/app/node_modules/mongoose/lib/query.js:1151:11)
    2015-02-01T20:38:20.903083+00:00 app[web.1]:     at Object._onImmediate (/app/node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)
    2015-02-01T20:38:20.922158+00:00 app[web.1]: error: Forever detected script exited with code: 8
    2015-02-01T20:38:21.737499+00:00 app[web.1]: Mean app started on port 55934 (development)

以下是未格式化的完整代码,希望有人看到破坏循环的细微差别:

User.find().where({
    code: req.body.Body.toUpperCase()
}).exec(function (err, sender1) {
    // Who sent the message?

    console.log('this is sender1 length ', sender1.length);

    for (var jj = 0; jj < sender1.length; jj = jj+1) {
        console.log('The for loop executed!');
    console.log('this is sender1 section', sender1[jj].section);
    console.log('jj is', jj);
        User.findOne({
            phone: req.body.From,
            section: sender1[jj].section
        }, function (err, from1) {
          console.log('This is the from1 phone',from1.phone);
          console.log('This is the section1 phone',sender1[jj].phone);

0 个答案:

没有答案
相关问题