[英]Nodejs - How to route by name in url with express and mongoose?
[英]NodeJS Express/Mongoose route confusion
我一定不能正确理解这里的内容。 我的路线看起来像这样
function(req,res){
var thingId = validate.thingId(req.body) ? req.body.thingId : res.send(400,'Invalid Thing');
var newBlock = new Block({
thing : mongoose.Types.ObjectId(thingId)
}).save();
}
我在req.body.thingId中传入一个空字符串以测试验证功能。 它返回应有的false,服务器将返回应有的400错误,但是我收到错误“传递的参数必须是12个字节的单个字符串或24个十六进制字符的字符串”。 (它在谈论模型ID)。
因此,显然仍在创建newBlock并将thingId传递给mongoose.Types.ObjectId函数。
我(可能是错误的)印象是,调用res.send本质上就像是“返回”,并且在它无法运行之后进行编码。 我看不到这可能是一个异步问题,有人能指出我正确的方向吗?
问题是res.send()不会从函数返回执行。 您应该使用代码块,如果验证失败,则返回。
我做了user3334561所说的,这可行。
var thingId;
if (validate.thingId(req.body)) thingId = req.body.thingId;
else return res.send(400,'Invalid Thing');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.