nodejs中的变量范围误解

时间:2015-08-11 20:58:59

标签: javascript arrays node.js loops promise

我无法理解为什么从另一个函数返回一个数组仅限于一个代码块。 IE:

exports.join = function(req, res){
  User.findById(req.user._id, function(err, user) {
     var dupe = []; //placeholder array
     var arr = user.forms_container.joinList;
     var title = user.forms_container.title;
     arr.forEach(function (rep) {
      //console.log(rep);
      Report.findById(rep).deepPopulate('subreport.subreport.subreport.subreport.subreport.subreport').execAsync()
      .then(function(doc) {
        //console.log(doc);
        dupe.push(doc);
        treeCycle(doc, dupe);
        console.log(dupe);
      }).catch(function(err) {
        throw err;
      });
      tracker = tracker + 1;

      if(tracker == arr.length) {
        console.log(dupe); //returns [];
        var counter = 0;
        var masterbody;
        for(var x = 0; x < dupe.length; x++) {
           masterbody = masterbody + dupe[x].body;
           counter = counter + 1;
        }
        if(counter==dupe.length) {
          newDoc.body = masterbody;
          //newDoc.save();
          res.json(newDoc);
        }
      }
     });

  });
};

有一次,我调用treeCycle,它将遍历该函数(递归函数)并开始填充数组“dupe”,如果我在控制台中记录它之后,它会显示它已完成,这里是控制台C / p'd:

[ { _id: 55c35cd942dd12ec19b30f2e,
    owner: 55a80b8404dc0dc8187395d8,
    title: 'Chrome1',
    date: 'Thu Aug 06 2015 09:10:49 GMT-0400 (Eastern Daylight Time)',
    body: '123213',
    __v: 0,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] } ]
[ { _id: 55c35cd942dd12ec19b30f2e,
    owner: 55a80b8404dc0dc8187395d8,
    title: 'Chrome1',
    date: 'Thu Aug 06 2015 09:10:49 GMT-0400 (Eastern Daylight Time)',
    body: '123213',
    __v: 0,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] },
  { _id: 55c8d774e904bc141681e115,
    body: 'dddd',
    owner: 55a80b8404dc0dc8187395d8,
    date: 'Mon Aug 10 2015 12:55:16 GMT-0400 (Eastern Daylight Time)',
    title: 'herdrute',
    __v: 1,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] } ]
[ { _id: 55c35cd942dd12ec19b30f2e,
    owner: 55a80b8404dc0dc8187395d8,
    title: 'Chrome1',
    date: 'Thu Aug 06 2015 09:10:49 GMT-0400 (Eastern Daylight Time)',
    body: '123213',
    __v: 0,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] },
  { _id: 55c8d774e904bc141681e115,
    body: 'dddd',
    owner: 55a80b8404dc0dc8187395d8,
    date: 'Mon Aug 10 2015 12:55:16 GMT-0400 (Eastern Daylight Time)',
    title: 'herdrute',
    __v: 1,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] },
  { _id: 55bf7465c60c959015dee0ed,
    owner: 55a80b8404dc0dc8187395d8,
    title: 'ffefeeffefeeffefefe',
    date: 'Mon Aug 03 2015 10:02:13 GMT-0400 (Eastern Daylight Time)',
    body: 'efefefefef',
    __v: 0,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] } ]
[ { _id: 55c35cd942dd12ec19b30f2e,
    owner: 55a80b8404dc0dc8187395d8,
    title: 'Chrome1',
    date: 'Thu Aug 06 2015 09:10:49 GMT-0400 (Eastern Daylight Time)',
    body: '123213',
    __v: 0,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] },
  { _id: 55c8d774e904bc141681e115,
    body: 'dddd',
    owner: 55a80b8404dc0dc8187395d8,
    date: 'Mon Aug 10 2015 12:55:16 GMT-0400 (Eastern Daylight Time)',
    title: 'herdrute',
    __v: 1,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] },
  { _id: 55bf7465c60c959015dee0ed,
    owner: 55a80b8404dc0dc8187395d8,
    title: 'ffefeeffefeeffefefe',
    date: 'Mon Aug 03 2015 10:02:13 GMT-0400 (Eastern Daylight Time)',
    body: 'efefefefef',
    __v: 0,
    isCompleted: false,
    subreport: [],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] },
  { _id: 55c361705d00464419a1d40c,
    owner: 55a80b8404dc0dc8187395d8,
    title: 'IE11',
    date: 'Thu Aug 06 2015 09:30:24 GMT-0400 (Eastern Daylight Time)',
    body: 'PEWPEWPEWPW',
    __v: 1,
    isCompleted: false,
    subreport:
     [ { _id: 55ca4d5456f8cdb416d36092,
         body: 'testest',
         owner: 55a80b8404dc0dc8187395d8,
         date: 'Tue Aug 11 2015 15:30:28 GMT-0400 (Eastern Daylight Time)',
         title: 'Sub1',
         __v: 2,
         parentReport: 55c361705d00464419a1d40c,
         isCompleted: false,
         subreport: [Object],
         authors: [Object],
         author: [Object] } ],
    authors: [ 'Karan Kotwal' ],
    author: [ 55a80b8404dc0dc8187395d8 ] },
  { _id: 55ca4d5456f8cdb416d36092,
    body: 'testest',
    owner: 55a80b8404dc0dc8187395d8,
    date: 'Tue Aug 11 2015 15:30:28 GMT-0400 (Eastern Daylight Time)',
    title: 'Sub1',
    __v: 2,
    parentReport: 55c361705d00464419a1d40c,
    isCompleted: false,
    subreport:
     [ { _id: 55ca53728ecc9400150610b6,
         body: 'sadsadasdasdasdadasdsadasdasEnter subreport here',
         owner: 55a80b8404dc0dc8187395d8,
         date: 'Tue Aug 11 2015 15:56:34 GMT-0400 (Eastern Daylight Time)',
         title: 'Sub1\'s sub1',
         __v: 1,
         parentReport: 55ca4d5456f8cdb416d36092,
         isCompleted: false,
         subreport: [],
         authors: [Object],
         author: [Object] } ],
    authors: [ 'Jeff The man' ],
    author: [ 55ad14bac2419660021d80b0 ] },
  { _id: 55ca53728ecc9400150610b6,
    body: 'sadsadasdasdasdadasdsadasdasEnter subreport here',
    owner: 55a80b8404dc0dc8187395d8,
    date: 'Tue Aug 11 2015 15:56:34 GMT-0400 (Eastern Daylight Time)',
    title: 'Sub1\'s sub1',
    __v: 1,
    parentReport: 55ca4d5456f8cdb416d36092,
    isCompleted: false,
    subreport: [],
    authors: [ 'Empty Empty' ],
    author: [ 55bbbc20877f48a01bf0c8d9 ] } ]

但是这样做,调用控制台获取数组dupe的最终版本将返回空。我在块if(tracker == array.length);中执行它我认为它的范围将贯穿整个函数,因为我在开始时初始化它。

TreeCycle:

function treeCycle(doc, arr) {
  if(doc.subreport.length != 0) {
    for (var x = 0; x < doc.subreport.length; x++){
      arr.push(doc.subreport[x]);
      //console.log(arr);
      if(doc.subreport[x].subreport.length != 0) {
        treeCycle(doc.subreport[x], arr);
      }
    }
  }
  else {
    return arr;
  }
};

2 个答案:

答案 0 :(得分:1)

这似乎是一个异步问题,您的if(tracker == arr.length) {部分在树循环之前运行。完成异步任务后,您需要调用if(tracker == arr.length) {部分。

示例:

exports.join = function(req, res) {
    User.findById(req.user._id, function(err, user) {
        var dupe = []; //placeholder array
        var arr = user.forms_container.joinList;
        var title = user.forms_container.title;
        arr.forEach(function(rep) {
            //console.log(rep);
            Report.findById(rep).deepPopulate('subreport.subreport.subreport.subreport.subreport.subreport').execAsync()
                .then(function(doc) {
                    //console.log(doc);
                    dupe.push(doc);
                    treeCycle(doc, dupe);
                    console.log(dupe);

                    tracker = tracker + 1;

                    if (tracker == arr.length) {
                        console.log(dupe); //returns [];
                        var counter = 0;
                        var masterbody;
                        for (var x = 0; x < dupe.length; x++) {
                            masterbody = masterbody + dupe[x].body;
                            counter = counter + 1;
                        }
                        if (counter == dupe.length) {
                            newDoc.body = masterbody;
                            //newDoc.save();
                            res.json(newDoc);
                        }
                    }

                }).catch(function(err) {
                    throw err;
                });
        });

    });
};

答案 1 :(得分:0)

它很可能是您使用promises的方式的问题。我建议您阅读它们以了解它们的工作原理以及如何重构代码以便更好地使用它们。

如果您将if (tracker...阻止移动到.then(回调(或其他.then),则可能会有效。