2015-12-09 34 views
-3

大部分情况下我可能会犯一些错误,而且它太小以至于我无法识别它。与Async nodejs相关的问题 - 给出不准确的回应

代码片段 -

async.parallel 
([ 
function(callback) 
{ 
    pictureset.findOne({ 'jebno': newz }, function (err, docsss) 
    { 
     if (err) return callback(err); 
     pictures = docsss; 
     console.log(docsss); 
     callback(); 
    }); 

}, 

function(callback) 

{ 
    merchantmodel.findOne({ 'jebno': newz1 }, function (err, docss) 
    { 
     if (err) return callback(err); 
     merchantobject = docss; 
     console.log(docss); 
     callback(); 
    }); 

}, 

function(err) 
{ 

    console.log(pictures); 
    console.log(merchantobject); 
    console.log("We are here"); 
    res.json({ picturemodel: pictures, merchantobject: merchantobject, status: 100, message: 'Successfully Done1'});    

    if (err) return next(err); 
} 
]); 

现在我期待这样的反应。

Value from console.log(docsss); 
Value from console.log(docss); 
Value from console.log(pictures); 
Value from console.log(merchantobject); 
we are here 

但我我的控制台上得到响应是

  1. 未定义
  2. 未定义
  3. 我们在这里
  4. 我以前见过一些阵列相关的消息,但没”在这些情况下真的会改变结果。我对它做了一些研究,但没有明白它的确切含义。从执行console.log
  5. 值(docsss)&的console.log(docss)

这里是控制台的实际片断

undefined 
undefined 
We are here 
function() { 
      var length = Math.max(arguments.length - startIndex, 0); 
      var rest = Array(length); 
      for (var index = 0; index < length; index++) { 
       rest[index] = arguments[index + startIndex]; 
      } 
      switch (startIndex) { 
       case 0: return func.call(this, rest); 
       case 1: return func.call(this, arguments[0], rest); 
      } 
      // Currently unused but handle cases outside of the switch statement: 
      // var args = Array(startIndex + 1); 
      // for (index = 0; index < startIndex; index++) { 
      //  args[index] = arguments[index]; 
      // } 
      // args[startIndex] = rest; 
      // return func.apply(this, args); 
     } 
{ _id: 5612c8950e1489f419ae1f0f, 
    jebno: '1231checka', 
    __v: 0, 
    photos: 
    [ '1445524441140_12023123_10156249375555727_859908445_n.jpg', 
    '1445524452856_12063857_919875394745615_3655186829888867333_n.jpg', 
    '1445524452873_491676259.jpg', 
    '1445524482917_12023123_10156249375555727_859908445_n.jpg', 
    '1445524894340_7a668c73cddcd2050821f83be901832a_1426070017.jpg', 
    '1445524894365_577161_424797084279112_1944605947_n.jpg', 
    '1445525002813_12063857_919875394745615_3655186829888867333_n.jpg' ] } 
{ _id: 56645b9b29422ebad43b59be, 
    name: 'Ramesh Sharma', 
    email: '[email protected]', 
    password: 'ramesh', 
    jebno: '1455', 
    mobileno: '123456754', 
    address: 'Ramesh Chowk', 
    coverphoto: '1449689932496_12243392_10153773144324749_4504520513350378845_n.jpg', 
    ratings: 4, 
    totalratings: 12 } 

回答

1

async.parallel确实带有一个回调函数,而不是作为最后一个数组元素的回调函数。

您的代码应该是这样的:

async.parallel([ 
    function(callback) { 
     pictureset.findOne({ 'jebno': newz }, callback); 
    }, 
    function(callback) { 
     merchantmodel.findOne({ 'jebno': newz1 }, callback); 
    } 
], function(err, results) { 
    if (err) return next(err); 

    var pictures = results[0], 
     merchantobject = results[1]; 
    console.log(pictures); 
    console.log(merchantobject); 
    console.log("We are here"); 
    res.json({ 
     picturemodel: pictures, 
     merchantobject: merchantobject, 
     status: 100, 
     message: 'Successfully Done1' 
    });    
}); 
+0

非常感谢您的时间Bergi。这个解决方案如何与其他人发布的不同?我很好奇,我会对你会给予的观点做更多的研究。 –

+1

@RananathanSwamy:它不会记录对象两次,但它更加高效和简洁(并且不需要那些外部变量 - 你确实在某处声明它们,对吗?)。我也移动了错误处理代码,以便在发生错误时不发送响应,这可能更干净。 – Bergi

+0

令人惊叹的_/\ _。万分感谢。我不能接受两个答案真的很伤心。 @ idbehold的答案也是足够的。 –

1

你需要把最终的回调函数作为在想要并行运行的回调函数数组之后的第二个参数:

async.parallel([ 
    function(callback){ 
     pictureset.findOne({ 'jebno': newz }, function (err, docsss){ 
      if (err) return callback(err); 
      pictures = docsss; 
      console.log(docsss); 
      callback(); 
     }); 
    }, 
    function(callback){ 
     merchantmodel.findOne({ 'jebno': newz1 }, function (err, docss){ 
      if (err) return callback(err); 
      merchantobject = docss; 
      console.log(docss); 
      callback(); 
     }); 
    } 
], 
// optional callback 
function(err, results){ 
    console.log(pictures); 
    console.log(merchantobject); 
    console.log("We are here"); 
    res.json({ picturemodel: pictures, merchantobject: merchantobject, status: 100, message: 'Successfully Done'});    

    if (err) return next(err); 
}); 
+0

谢谢你的时间idbehold。我已经尝试过你的解决方案,它正常工作。我想在这里添加同样的东西,这个解决方案与其他人发布的解决方案有什么不同?我很好奇,我会更详细地介绍你要放下的几点。谢谢 –

1

阮冈纳赞,无论是基本的答案说明了同样的事情的解释如下:

您传递的功能async.parallel数组。它同时运行它们。你不知道他们将以什么顺序开始,或者什么时候结束。

作为答案指出的最后一个函数需要在数组之外,是最终的回调函数。它在两种情况下被调用:

一,所有传递到它上面的数组中的函数已成功完成。如果您在每个函数的回调中提供了一些数据,那么这些数据将显示在结果数组中。错误对象(err)将为空。

二,如果出现任何错误,则传递给最终函数的错误对象(err)将包含实际错误。您需要在代码中以某种方式处理该错误。

+0

@ Bergi的后续评论帮助我理解了这一点,并且我对这个主题做了更多的研究。感谢您的详细解释。 –