等待function,然后控制台的结果

我想等我的function先取得所有结果,然后安慰我的结果。

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 ] 

之后,添加控制台内的function,其结果如下:

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 } ] 

       

网上收集的解决方案 "等待function,然后控制台的结果"

尝试下面的代码:

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

如果它仍然不工作,请尝试使用函数fetchingImagefetchingBrand callback函数。 或者你也可以尝试在eachSeries使用eachSeries

编辑:-

asynchronouseachseries

callback改变你的function。

 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数组。 请参阅文档

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