我想显示mongodb集合中的数据
我正在使用pug并想在mongodb中显示我的数据收集
但是我不知道这个错误
我已经尝试过
这是我的.pug文件
extends layout
block content
if registrations.length
table
tr
th Name
th Email
each registration in registrations
tr
td=registration.name
td=registration.email
else
p No registrations yet
这是我的index.js文件
const Registration = mongoose.model('Registration');
router.get('/registrations', (req, res) => {
Registration.find()
.then((registrations) => {
res.render('index', { title: 'Listing registrations' });
})
.catch(() => { res.send('Something went wrong'); });
});
这是错误消息
TypeError: E:\web_apps_project\demo-node-app\views\index.pug:4
2|
3| block content
> 4| if registrations.length
5| table
6| tr
7| th Name
Cannot read property 'length' of undefined
at eval (eval at wrap (E:\web_apps_project\demo-node-app\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:21:19)
at template (eval at wrap (E:\web_apps_project\demo-node-app\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:79:50)
at Object.exports.renderFile (E:\web_apps_project\demo-node-app\node_modules\pug\lib\index.js:427:38)
at Object.exports.renderFile (E:\web_apps_project\demo-node-app\node_modules\pug\lib\index.js:417:21)
at View.exports.__express [as engine] (E:\web_apps_project\demo-node-app\node_modules\pug\lib\index.js:464:11)
at View.render (E:\web_apps_project\demo-node-app\node_modules\express\lib\view.js:135:8)
at tryRender (E:\web_apps_project\demo-node-app\node_modules\express\lib\application.js:640:10)
at Function.render (E:\web_apps_project\demo-node-app\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (E:\web_apps_project\demo-node-app\node_modules\express\lib\response.js:1012:7)
at Registration.find.then (E:\web_apps_project\demo-node-app\routes\index.js:36:17)
at process._tickCallback (internal/process/next_tick.js:68:7)
答案 0 :(得分:0)
您需要将数据库查询返回的注册信息传递给模板,如下所示:
router.get('/registrations', (req, res) => {
Registration.find()
.then((registrations) => {
res.render('index', { title: 'Listing registrations', 'registrations': registrations });
})
.catch(() => { res.send('Something went wrong'); });
});