以下是我的代码,我首先验证用户是否存在于组中,如果存在,则将帐户推入阵列并保存。这是通过/ addaccount上的POST请求完成的。
groupRouter.post('/addaccount', Verify.verifyOrdinaryUser, function(req, res, next) {
Groups.findById(req.body.group, function(err,group) {
if (err) next(err);
var checkUser = function(user) {
for(var i=0; i<group.users.length; i++)
{
if(group.users[i]==user)
return true;
}
return false;
}
if(checkUser(req.decoded._doc._id)){
User.find({mobile:{$in:req.body.split}}, function(err, users) {
if(err) next(err); var flag = true;
if(users.length == req.body.split.length ) {
for(var i=0; i<users.length; i++) {
if(!checkUser(''+users[i]._id)) {
flag = false;
break;
}
}
if(flag) {
var myObject = {};
myObject.amount = req.body.amount;
myObject.by = req.decoded._doc._id;
myObject.split = req.body.split;
group.accounts.push(myObject);
group.save(function (err, groups) {
if(err) next(err);
console.log('Added entries');
res.json(groups);
})
}
else
res.end('All users not in the group');
}
else
res.end('Split users do not exist');
})
}
else
res.end('No Permission');
})
})
现在当我输入一个用户数组并尝试推送一个对象并通过 代码如下:
if(flag) {
var myObject = {};
myObject.amount = req.body.amount;
myObject.by = req.decoded._doc._id;
myObject.split = req.body.split;
group.accounts.push(myObject);
group.save(function (err, groups) {
if(err) next(err);
console.log('Added entries');
res.json(groups);
})
}
我收到错误消息:错误:发送后无法设置标头。如下图所示。
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.header (D:\Ionic\PocketBuddies\node_modules\express\lib\response.js:718:10)
at ServerResponse.send (D:\Ionic\PocketBuddies\node_modules\express\lib\response.js:163:12)
at done (D:\Ionic\PocketBuddies\node_modules\express\lib\response.js:957:10)
at Object.exports.renderFile (D:\Ionic\PocketBuddies\node_modules\jade\lib\index.js:374:12)
at View.exports.__express [as engine] (D:\Ionic\PocketBuddies\node_modules\jade\lib\index.js:417:11)
at View.render (D:\Ionic\PocketBuddies\node_modules\express\lib\view.js:126:8)
at tryRender (D:\Ionic\PocketBuddies\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (D:\Ionic\PocketBuddies\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (D:\Ionic\PocketBuddies\node_modules\express\lib\response.js:961:7)
at D:\Ionic\PocketBuddies\app.js:78:7
at Layer.handle_error (D:\Ionic\PocketBuddies\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (D:\Ionic\PocketBuddies\node_modules\express\lib\router\index.js:310:13)
at D:\Ionic\PocketBuddies\node_modules\express\lib\router\index.js:280:7
at Function.process_params (D:\Ionic\PocketBuddies\node_modules\express\lib\router\index.js:330:12)
at IncomingMessage.next (D:\Ionic\PocketBuddies\node_modules\express\lib\router\index.js:271:10)
我搜索了这个问题,它说我无法在res.end()之后更新响应,但是当我调用res.json时,我没有看到任何res.end()被调用。 请帮我在这里找到一个问题。 我正在研究一个项目,这个错误让我发疯。
提前致谢
答案 0 :(得分:0)
正如robertklep正确指出的那样,这里的问题是没有返回错误,因此在group.save()函数发生错误后它会呈现错误页面并且它会点击res.json ()导致错误。 如果没有错误,它可以正常工作。 代码需要按如下方式更新,以便在错误发生时返回错误,而不是转移到res.json()部分。
group.save(function (err, groups) {
if(err) return next(err);
console.log('Added entries');
res.json(groups);
})
通过向我提供实际错误,然后在事情正常工作后必须纠正错误,这对我来说是个窍门。