我有一条似乎拒绝渲染 Jade 模板的快速路线。当我输入错误的 Jade 模板字符串时,我得到通常无法找到正在渲染到浏览器的文件。
但是当我输入正确的内容时,什么也没有发生......
此外,当我在 res.render()
之后放置 res.status(500).send()
时,我最终会收到 500 错误或我选择放入其中的任何其他代码。
这是怎么回事?
app.post('/stuff/new', function (req,res){
Stuff.findById(req.body.booking_id, function(err, stuff){
if(err){ console.log("Err", err)}
else if (booking) {
Thungs.findById(req.body.user_id, function(err, thung){
user.stuff.push(req.body.things);
user.save();
});
Thingers.findById(req.body.center_id, function(err, thinger){
center.stuff.push(req.body.things);
center.save();
});
stuff.prop = 'thing';
stuff.save(function(err, result){
if (err) { console.err("err", err) }
else {
res.render('booking/thank_you', {
url: req.url,
id: "thank_you"
}, function(err, html){
if (err) { console.err("ERR", err) }
else {
res.send(html);
}
});
res.status(500).send();
}
});
}
})
});
最佳答案
如果您只想渲染模板,则不需要 res.send(html);
,只需从 res.render()
中删除回调即可正常工作。另外,当 res.status(500).send();
显示 500 时,因为在 res.render()
和 res.send()
之后连接不会终止,它们后面的语句将执行,您可以使用 return res.render()
和 return res.send()
那么控制将不会转到 res.status(500)。发送()
希望有帮助:)
关于javascript - Res.render() 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36144081/