与Async nodejs相关的问题-给出不准确的响应

Issue related to Async nodejs - giving inaccurate response

本文关键字:不准确 响应 问题 nodejs Async      更新时间:2023-09-26

很可能是我犯了一些错误,而且它太小了,我无法识别它。

代码段-

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. 我以前看到过一些与数组相关的消息,但在这些情况下并没有真正改变结果。我对它做了一些研究,但没能理解它的确切含义
  5. console.log(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: 'ramesh@gmail.com',
  password: 'ramesh',
  jebno: '1455',
  mobileno: '123456754',
  address: 'Ramesh Chowk',
  coverphoto: '1449689932496_12243392_10153773144324749_4504520513350378845_n.jpg',
  ratings: 4,
  totalratings: 12 }

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'
    });            
});

您需要将最后一个回调函数作为第二个参数放在要并行运行的回调函数数组之后:

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);
});

Ranganathan,两个答案本质上说明了相同的事情,解释如下:

您将一个函数数组传递给async.parallet。它会同时运行所有函数。你不知道它们将以什么顺序开始,也不知道它们何时结束。

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

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

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