报告一个逻辑bug
如果提交评论的模板代码如下
form(method="POST", action="/user/comment")
input(type="hidden", name="comment[movie]", value="#{movie._id}")
input(type="hidden", name="comment[from]", value="#{user._id}")
.form-group
textarea.form-control(name="comment[content]", rows="4")
.pull-right
button.btn.btn-primary(type="submit") 提交在用户没有登录的情况下会报如下错误:
TypeError: Cannot read property '_id' of undefined
因为在用户没有登录的情况下,app.locals.user = _user 的值是undefined,所以上面表单的user._id自然是undefined。
我的解决办法:
if user
input(type="hidden", name="comment[from]", value="#{user._id}")即在用户登录的情况下,才读取user._id