nodejs 是否支持在一个路由器中更新多个集合?
Does nodejs supports updating many collection in one router?
我尝试在一个路由器中更新我的两个集合,但它一直给我错误:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)
at ServerResponse.setHeader (_http_outgoing.js:501:3)
这是我所做的:
api.js
router.put('/editbloodrequest', function(req, res) {
//this works
Bloodrequest.findOne({ _id:"5c1d2c8c68503b0adceffa92"}, function(err, bloodrequest) {
if (err) throw err;
bloodrequest.request_status = "claimed";
});
//but after inserting this, it gives me error
Blooddonation.findOne({ _id:"5c00fa03dadb0c3b00739dd9"}, function(err, blooddonation) {
if (err) throw err;
blooddonation.blood_group = "test";
});
});
首先,我强烈建议您放弃回调并改用 Mongo 的 Promise 行为。除非您打算同时执行这两个请求,否则这将是您使用 promises 表达这一点的方式:
Bloodrequest.findOne({ _id:"..." }).then(bloodrequest => {
bloodrequest.request_status = "claimed";
})
.then(() => Blooddonation.findOne({ _id:"..." }).then(blooddonation => {
blooddonation.blood_group = "test";
});
其次,我猜您从您的示例中删除了一些代码;您实际上在任何地方都没有回应(使用 res
)。根据错误,我猜您试图在回调的 both 中设置 res
。您只能回复一次,该错误通常意味着您已经重定向/回复/等等,现在正在尝试再次回复。
通常你会这样做:
Bloodrequest.findOne({ _id:"..." }).then(bloodrequest => {
bloodrequest.request_status = "claimed";
})
.then(() => Blooddonation.findOne({ _id:"..." }).then(blooddonation => {
blooddonation.blood_group = "test";
}).then(() => {
// set your positive res response
}).catch(error => {
// set your error res response
// since you're using promises, if any of the above steps fail, you'll
// end up here.
});
您需要需要要在 api.js
中更新的集合模型
在路由处理函数中,您需要在相关集合之间进行交易。你可以 fawn npm 使这更容易
https://www.npmjs.com/package/fawn
我尝试在一个路由器中更新我的两个集合,但它一直给我错误:
Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:494:11) at ServerResponse.setHeader (_http_outgoing.js:501:3)
这是我所做的:
api.js
router.put('/editbloodrequest', function(req, res) {
//this works
Bloodrequest.findOne({ _id:"5c1d2c8c68503b0adceffa92"}, function(err, bloodrequest) {
if (err) throw err;
bloodrequest.request_status = "claimed";
});
//but after inserting this, it gives me error
Blooddonation.findOne({ _id:"5c00fa03dadb0c3b00739dd9"}, function(err, blooddonation) {
if (err) throw err;
blooddonation.blood_group = "test";
});
});
首先,我强烈建议您放弃回调并改用 Mongo 的 Promise 行为。除非您打算同时执行这两个请求,否则这将是您使用 promises 表达这一点的方式:
Bloodrequest.findOne({ _id:"..." }).then(bloodrequest => {
bloodrequest.request_status = "claimed";
})
.then(() => Blooddonation.findOne({ _id:"..." }).then(blooddonation => {
blooddonation.blood_group = "test";
});
其次,我猜您从您的示例中删除了一些代码;您实际上在任何地方都没有回应(使用 res
)。根据错误,我猜您试图在回调的 both 中设置 res
。您只能回复一次,该错误通常意味着您已经重定向/回复/等等,现在正在尝试再次回复。
通常你会这样做:
Bloodrequest.findOne({ _id:"..." }).then(bloodrequest => {
bloodrequest.request_status = "claimed";
})
.then(() => Blooddonation.findOne({ _id:"..." }).then(blooddonation => {
blooddonation.blood_group = "test";
}).then(() => {
// set your positive res response
}).catch(error => {
// set your error res response
// since you're using promises, if any of the above steps fail, you'll
// end up here.
});
您需要需要要在 api.js
中更新的集合模型在路由处理函数中,您需要在相关集合之间进行交易。你可以 fawn npm 使这更容易 https://www.npmjs.com/package/fawn