等待功能,然后控制台结果

时间:2016-08-25 06:52:32

标签: node.js mongodb loops

我想等待我的功能,比如首先获取所有结果,然后安慰我的结果。

exports.listofAllFeaturedProd = (req, res) => {

  Product.find({is_featured:'1'},function(error,fetchallFeatProds)
  {

    var allProducts = new Array();
    var pp = 0;
    var products  = new Array();
    for (var ProductId in fetchallFeatProds)
    {
        var pArr            = [];
        pArr['_id']         = fetchallFeatProds[ProductId]._id;
        pArr['name']        = fetchallFeatProds[ProductId].name;
        pArr['sku']         = fetchallFeatProds[ProductId].sku;
        pArr['description'] = fetchallFeatProds[ProductId].description;
        pArr['price']       = fetchallFeatProds[ProductId].price;
        pArr['large_image'] = fetchingImage(fetchallFeatProds[ProductId]._id);
        pArr['brand']       = fetchingBrand(fetchallFeatProds[ProductId].brand_id);

        console.log('#################### IMAGE ####################');   
        console.log(pArr); 
    pp++;
    }
    console.log(products); 

  });

}; 


function fetchingImage(pid)
{
  ProductImage.findOne({product_id:pid},function(error,fetchallFeatProdsImgs)
  {
    console.log(fetchallFeatProdsImgs.large_image);
     return fetchallFeatProdsImgs.large_image;
  });
}

function fetchingBrand(bid)
{
  Brand.findOne({_id:bid},function(error,fetchAllBrands)
  { 
    console.log(fetchAllBrands);
      return fetchAllBrands;

  });
}

节点不等待函数和控制台在该控制台之后未定义我的函数结果。我如何停止我的代码获取第一个结果然后控制数组中的所有数据。

console.log(pArr);

的输出
[ _id: 57bd996ebf8c930b2bcc06a1,
  name: 'New Product',
  sku: 'New-Product',
  description: 'New Product',
  price: 'test',
  large_image: undefined,
  brand: undefined ]

之后在功能中添加了控制台,其结果如下:

fetchingImage

的输出
images/12.png

fetchingBrand

的输出
{ user_id: '57b42b571fc35e49162de413',
  brand_name: '10 Fork ',
  brand_logo: 'uploads/brands_logo/1472027911329_5.png',
  brand_desc: '10 Fork',
  _id: 57bd5ce6cebed2a3189cedcf,
  __v: 0 }

所需的输出是:

[ _id: 57bd996ebf8c930b2bcc06a1,
  name: 'New Product',
  sku: 'New-Product',
  description: 'New Product',
  price: 'test',
  large_image: images/12.png,
  brand: { user_id: '57b42b571fc35e49162de413',
           brand_name: '10 Fork ',
           brand_logo: 'uploads/brands_logo/1472027911329_5.png',
           brand_desc: '10 Fork',
           _id: 57bd5ce6cebed2a3189cedcf,
           __v: 0 } ]

1 个答案:

答案 0 :(得分:2)

尝试以下代码:

var temp = [], 
     async = require('async');

async.eachSeries(fetchallFeatProds, function(ProductId, callback)
{
   pArr['_id']         = ProductId._id;
   pArr['name']        = ProductId.name;
   pArr['sku']         = ProductId.sku;
   pArr['description'] = ProductId.description;
   pArr['price']       = ProductId.price;
   pArr['large_image'] = fetchingImage(ProductId._id);
   pArr['brand']       = fetchingBrand(ProductId.brand_id);

   temp.push(pArr);
   callback(null);
}, function(err){
    console.log(temp); //This should give you desired result
});

如果它仍然无效,请尝试使用callback功能fetchingImagefetchingBrand。或者您也可以尝试在async.parallel内使用eachSeries

编辑: -

async-eachseries

使用callback更改您的功能。

function fetchingImage(pid, callback)
{
    ProductImage.findOne({product_id:pid},function(error,fetchallFeatProdsImgs)
   {
      console.log(fetchallFeatProdsImgs.large_image);
     callback(error,fetchallFeatProdsImgs.large_image);
   });
}

function fetchingBrand(bid, callback)
{
    Brand.findOne({_id:bid},function(error,fetchAllBrands)
    { 
       console.log(fetchAllBrands);
       calback(error,fetchAllBrands);
   });
}

使用async.parallel使其等到两个功能都完成。然后进入temp数组。 Doc to refer

async.eachSeries(fetchallFeatProds, function(ProductId, callback)
{
   var pArr  = {};
   pArr['_id']         = ProductId._id;
   pArr['name']        = ProductId.name;
   pArr['sku']         = ProductId.sku;
   pArr['description'] = ProductId.description;
   pArr['price']       = ProductId.price;
   async.parallel([
     function(callback)
     {
          fetchingImage(ProductId._id, function(err, res){
         pArr['large_image'] = res;
         callback(err); //Forgot to add
      });
   },
    function(callback)
   {
      fetchingBrand(ProductId.brand_id,function(err, res){
         pArr['brand'] = res;
         callback(err); //Forgot to add
      });
   },
], function(err){

   console.log(pArr); //Edit
   temp.push(pArr);
   callback(err); 
})
  }, function(err){
    console.log(temp); //This should give you desired result
    callback(err);
});