javascript - 为什么在 pug 模板中迭代时出现“无法读取未定义的属性‘长度’”错误?
问题描述
我正在用 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"
}]
});
推荐阅读
- reactjs - 更改 iframe 中的 JITSI MEET REACT waterlink 标志
- python - 使用 pandas 数据框访问 csv 文件
- random-forest - MLP和RF的变量排名不应该相同吗?
- angular - Angular:在 *ngIf 失败的情况下不显示任何内容
- sql-server - 为什么 sqlcmd 不导入任何东西?
- angular - 预选日期在 ngx-daterangepicker-material 中不起作用
- java - 一段时间内的数据输出
- arcore - 使用变换矩阵创建锚点
- sql - 如何使用单个 SQL 聚合函数查询为同一个聚合函数获取多个结果?
- jquery - 在 laravel 8 中使用服务器端处理隐藏数据表中的行