gpt4 book ai didi

node.js - 等待函数然后控制台结果

转载 作者:可可西里 更新时间:2023-11-01 10:21:23 26 4
gpt4 key购买 nike

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

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;

});
}

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

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

最佳答案

试试下面的代码:

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 中使用 async.parallel

编辑:-

async-eachseries

使用回调改变你的功能。

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

关于node.js - 等待函数然后控制台结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39138537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com