未传递到视图模板的用户数组
Array of users not passing to the view template
我有这样的路线并且工作正常:
// Dashboard route
router.get('/dashboard', ensureAuthenticated, (req, res) => {
Note.find({user: req.user.id})
.then(notes => {
res.render('index/dashboard', {
notes: notes
});
});
});
在 dashboard.handlebars 我可以循环数组:
{{#if notes}}
...
{{#each notes}}
{{title}}
{{body}}
{{/each}}
{{/if}}
我也有其他路线,几乎相同,但没有将数组传递给模板:
// Admin Route
router.get('/admin', ensureAdmin, (req, res) => {
User.find({})
.then(users => {
console.log("Users count is: " + users.length)
console.log(users)
res.render('index/admin'), {
users: users
};
});
});
在控制台上,我使 console.log 都正常工作,我得到了用户数量和信息。所以 Find() 正在工作,但数组似乎没有传递给模板。
我怀疑可能是因为我有一个全局变量:
res.locals.user = req.user || null;
保留记录器用户的信息。但是我更改了 Find() 函数上的变量名称,但没有成功。
在模板上,我的结果总是 "No users found"
{{#if users}}
<p>There are users</p>
{{else}}
<p>No users found</p>
{{/if}}
有什么想法吗?
已解决,是一个")"写错了...
'index/admin')
was the cause
解决方案是:
// Admin Route
router.get('/admin', ensureAdmin, (req, res) => {
User.find({})
.then(users => {
console.log("Users count is: " + users.length)
console.log(users)
res.render('index/admin', {
users: users
});
});
});
我有这样的路线并且工作正常:
// Dashboard route
router.get('/dashboard', ensureAuthenticated, (req, res) => {
Note.find({user: req.user.id})
.then(notes => {
res.render('index/dashboard', {
notes: notes
});
});
});
在 dashboard.handlebars 我可以循环数组:
{{#if notes}}
...
{{#each notes}}
{{title}}
{{body}}
{{/each}}
{{/if}}
我也有其他路线,几乎相同,但没有将数组传递给模板:
// Admin Route
router.get('/admin', ensureAdmin, (req, res) => {
User.find({})
.then(users => {
console.log("Users count is: " + users.length)
console.log(users)
res.render('index/admin'), {
users: users
};
});
});
在控制台上,我使 console.log 都正常工作,我得到了用户数量和信息。所以 Find() 正在工作,但数组似乎没有传递给模板。
我怀疑可能是因为我有一个全局变量:
res.locals.user = req.user || null;
保留记录器用户的信息。但是我更改了 Find() 函数上的变量名称,但没有成功。
在模板上,我的结果总是 "No users found"
{{#if users}}
<p>There are users</p>
{{else}}
<p>No users found</p>
{{/if}}
有什么想法吗?
已解决,是一个")"写错了...
'index/admin') was the cause
解决方案是:
// Admin Route
router.get('/admin', ensureAdmin, (req, res) => {
User.find({})
.then(users => {
console.log("Users count is: " + users.length)
console.log(users)
res.render('index/admin', {
users: users
});
});
});