[英]Why do I get "TypeError: Cannot read property 'length' of undefined" when using length method
[英]Why do I get a "Cannot read property 'length' of undefined" error when iterating in a pug template?
我正在用 Node 和 Express 构建一些东西,如果用户没有登录,那么主页应该在导航栏中显示注册和登录链接。 一切看起来都应该有效,如果我不迭代,我可以显示信息。 但是当我使用迭代时,我收到此错误:
/home/michael/Desktop/budget-app/views/layout.pug:13 11| a(href='/') Simple Money Manager 12| if authLinks > 13| each val, index in authlinks 14| a(href='' + val) index 15| 16| // Content Cannot read property 'length' of undefined
这是render
方法:
res.render('home', {
authLinks: {
Signup: "/signup",
Login: "/login"
}
});
这是我的 pug 文件中使用的代码:
if authLinks
each val, index in authlinks
a(href='' + val) index
为什么 authLinks 未定义? 我完全不知所措。 谢谢您的帮助。
它应该是authLinks
而不是循环中的authlinks
if authLinks
each val, index in authLinks//here
a(href='' + val) index
编辑:您的 authLinks 是一个对象,而不是一个数组,也不能重复:
res.render('home', {
authLinks: [{
Signup: "/signup",
Login: "/login"
}]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.