[英]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.