gpt4 book ai didi

Node.js res.send VS res.end VS 返回res.end

转载 作者:可可西里 更新时间:2023-11-01 09:57:13 24 4
gpt4 key购买 nike

我有以下代码在 mongodb findAndUpdateOne 更新查询中有语法错误。

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 是否足以真正阻止代码到达结尾?

如果我没有以正确的方式停止错误,就会显示错误,可能不相关,但这里是:

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...

最佳答案

首先让我们看看三个ExpressJS函数有什么区别

res.end: 来自 NodeJS 核心。在 Express JS 中如果你需要快速结束请求并且不需要发送任何数据那么你可以使用这个函数

res.send:发送数据并结束请求

res.json 以JSON格式发送数据并结束请求。

My question is why is the code reaching the last part of the code:?

我希望您知道 JavaScript 是异步语言。使用 Mongoose 对 MongoDB 的所有数据库调用都是异步的。所以Compnay.findOne是一个异步函数调用,它停留在事件循环中,直到数据库读取操作未完成。作为 JS 的异步行为,JS 主线程执行不会等待 DB 函数结果返回(无阻塞)并到达最后一行,并且您的请求以调用 res.end("failed") 结束。但是当数据库读取操作完成返回数据时,您再次调用 res.send你以 Error: Can't set headers after they are sent. 结尾

希望这有帮助:)

关于Node.js res.send VS res.end VS 返回res.end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220467/

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