我有以下代碼在mongodb findAndUpdateOne更新查詢中有語法錯誤。Node.js res.send VS res.end VS return res.end
router.post('/buylicense', isLoggedIn, function(req, res) {
if (!req.body.cid) {
return res.send('failed');
}
Company.findOne({
ownedBy: req.user.local.username,
_id: req.body.cid
}, function(err, result) {
if (err) {
return res.send('failed');
}
if (result.license) {
return res.send('valid');
} else {
Company.findOneAndUpdate({
ownedBy: req.user.local.username,
_id: req.body.cid
}, {
license: true,
licenseExpireDate: {
$add: ["$date", 3 * 24 * 60 * 60000] // bad code, a problem for another day
}
}, function(err) {
if (err) {
console.log(err);
return res.end('failed'); // Code should stop here.
}
console.log('got here');
return res.send('success');
});
}
});
console.log('How did I get here?');
res.send('failed');
});
我的問題是,爲什麼是代碼到達代碼的最後一部分:
console.log('How did I get here?');
res.send('failed');
如果我用比return res.end
以外的任何代碼到達終點和崩潰我的應用程序。簡單地做res.end
不起作用,return res.send
也不起作用。 return
或至少res.end
是否足以實際阻止代碼到達最終?
顯示的錯誤,如果我不停止錯誤的right way
,可能不相關,但在這裏它是:
How did I get here?
{ CastError: Cast to date failed for value "[object Object]" at path "licenseExpireDate"
at MongooseError.CastError (/media/node_modules/mongoose/lib/error/cast.js:19:11)
at SchemaDate.cast (/media/node_modules/mongoose/lib/schema/date.js:242:9)
at SchemaDate.castForQuery (/media/node_modules/mongoose/lib/schema/date.js:276:17)
at Query._castUpdateVal (/media/node_modules/mongoose/lib/query.js:2477:17)
at Query._walkUpdatePath (/media/node_modules/mongoose/lib/query.js:2372:25)
at Query._castUpdate (/media/node_modules/mongoose/lib/query.js:2296:23)
at castDoc (/media/node_modules/mongoose/lib/query.js:2500:18)
at Query._findAndModify (/media/node_modules/mongoose/lib/query.js:1755:17)
at Query._findOneAndUpdate (/media/node_modules/mongoose/lib/query.js:1622:8)
at /media/node_modules/kareem/index.js:156:8
at args (/media/node_modules/kareem/index.js:71:20)
at Query.<anonymous> (/media/node_modules/mongoose/lib/schema.js:728:7)
at next (/media/node_modules/kareem/index.js:82:14)
at Kareem.execPre (/media/node_modules/kareem/index.js:99:3)
at Kareem.wrap (/media/node_modules/kareem/index.js:146:8)
at Query._findOneAndUpdate (/media/node_modules/kareem/index.js:188:11)
message: 'Cast to date failed for value "[object Object]" at path "licenseExpireDate"',
name: 'CastError',
kind: 'date',
value: { '$add': [ '$date', 259200000 ] },
path: 'licenseExpireDate',
reason: undefined }
got here
_http_outgoing.js:346
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
at ServerResponse.header (/media/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/media/node_modules/express/lib/response.js:163:12)
at /media/sf_vShared/xyz/app/modalRoutes.js:461:28
at /media/node_modules/kareem/index.js:160:11
at Query._findAndModify (/media/node_modules/mongoose/lib/query.js:1767:14)
at Query._findOneAndUpdate (/media/node_modules/mongoose/lib/query.js:1622:8)
at /media/node_modules/kareem/index.js:156:8
at args (/media/node_modules/kareem/index.js:71:20)
at Query.<anonymous> (/media/node_modules/mongoose/lib/schema.js:728:7)
at next (/media/node_modules/kareem/index.js:82:14)
at Kareem.execPre (/media/node_modules/kareem/index.js:99:3)
at Kareem.wrap (/media/node_modules/kareem/index.js:146:8)
at Query._findOneAndUpdate (/media/node_modules/kareem/index.js:188:11)
at Query.findOneAndUpdate (/media/node_modules/mongoose/lib/query.js:1611:15)
at Function.Model.findOneAndUpdate (/media/node_modules/mongoose/lib/model.js:1491:13)
[nodemon] app crashed - waiting for file changes before starting...
貓鼬的查找函數是異步的,這意味着它們在主線程,並在後臺單獨運行結束。在調用'Company.findOne'後的代碼會立即運行,您不應該在此處發送/發送。相反,如你所做的那樣,在回調中做到這一點。 –
因此,對於任何貓鼬操作,停止整個代碼繼續的方式是使用'return res.end'或者有更好的方法嗎? – Trax
返回結束任何功能,這不是特別表達或貓鼬的東西。 'res.send'和'end'是表示函數爲客戶返回數據的函數。 –