gpt4 book ai didi

javascript - async.each 无法在设置后设置 header

转载 作者:行者123 更新时间:2023-12-03 09:56:42 25 4
gpt4 key购买 nike

这就是发生的事情。我首先保存新公司,然后在保存之前将 _id 附加到每个新用户。我遇到的问题是返回响应。当我将 res.json() 放入重复的函数中时,显然我收到了一个错误,因为我已经收到了第一次循环时发送的响应。

那么,如何调用signupSeq(record, res),但等待异步方法完成,以便知道是否有错误?

    var signupSeq = function(req, res) {

async.waterfall([
function(callback) {
console.log(req);
if (req.company._id===undefined){
var company = new Company(req.company);
company.save(function(err){
if (err) {
console.log('save error');
callback(err);
}else{
callback(null, company._id);
}
})
}else{
callback(null, req.company._id); //pass teh plain ID if it's not a new name:xxx
}

},
function(companyId, callback) {

delete req.company
req.company = companyId
// Init Variables
var user = new User(req);
var message = null;

// Add missing user fields
user.provider = 'local';
user.displayName = user.firstName + ' ' + user.lastName;

// Then save the user
user.save(function(err) {
if (err) {
callback(err);

} else {
callback(null, user);
}
});
}
], function (err, result) {
if(err){
console.log(result+'funciton result')
return err
// res.status(400).send({
// message: errorHandler.getErrorMessage(err)
// });
}else{
console.log(result+'funciton result')
return result
//res.json(result)
}
});
}

exports.saveMany = function(req, res){
async.each(req.body, function(record, callback) {

// Perform operation on record.body here.
console.log('Processing record.body ' + record);

// Do work to process record.body here
var x = signupSeq(record, res)
console.log(x+'<<<<<<<value of x');
console.log('record.body processed');
callback();

}, function(err){
// if any of the record.body processing produced an error, err would equal that error
if( err ) {
res.json(err);
// One of the iterations produced an error.
// All processing will now stop.
console.log('A record.body failed to process');
} else {
res.json('Success');
console.log('All files have been processed successfully');
}
});
}

最佳答案

您可以在 signupSeg 函数中添加回调 (cb)。

var signupSeq = function(req, res, cb) {

async.waterfall([
function(callback) {
console.log(req);
if (req.company._id===undefined){
var company = new Company(req.company);
company.save(function(err){
if (err) {
console.log('save error');
callback(err);
}else{
callback(null, company._id);
}
})
}else{
callback(null, req.company._id); //pass teh plain ID if it's not a new name:xxx
}

},
function(companyId, callback) {

delete req.company
req.company = companyId
// Init Variables
var user = new User(req);
var message = null;

// Add missing user fields
user.provider = 'local';
user.displayName = user.firstName + ' ' + user.lastName;

// Then save the user
user.save(function(err) {
if (err) {
callback(err);

} else {
callback(null, user);
}
});
}
], function (err, result) {
if(err){
console.log(result+'funciton result')
cb(err)
// res.status(400).send({
// message: errorHandler.getErrorMessage(err)
// });
}else{
console.log(result+'funciton result')
cb(null,result)
//res.json(result)
}
});
}

exports.saveMany = function(req, res){
async.each(req.body, function(record, callback) {

// Perform operation on record.body here.
console.log('Processing record.body ' + record);

// Do work to process record.body here
signupSeq(record, res,function(err,result){
var x= result;
console.log(x+'<<<<<<<value of x');
console.log('record.body processed');
callback();
})


}, function(err){
// if any of the record.body processing produced an error, err would equal that error
if( err ) {
res.json(err);
// One of the iterations produced an error.
// All processing will now stop.
console.log('A record.body failed to process');
} else {
res.json('Success');
console.log('All files have been processed successfully');
}
});
}

这样,在 asyn.each 中,signipSeg 必须在回调() 调用之前完成。希望这会有所帮助。

关于javascript - async.each 无法在设置后设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30716800/

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