【发布时间】:2018-08-07 07:19:43
【问题描述】:
我已经成功在 mongodb compass 中保存了一些文档。我试图通过查询一些参数来检索值。
我知道 mongodb findOne 有诸如
query, fields(by default value: all), options and callback
但是操作的结果返回null并抛出类型错误。
这是我的代码:
router.get('/edit-page/:slug',(req,res) => {
console.log('slug:\t' + req.params.slug);
Pages.findOne({slug: req.params.slug},(errs, page) => {
if(errs){
req.flash('danger', 'page not found');
console.error(errs);
} else {
console.log(page);
res.render('admin/edit_page',{
title: page.title,
slug: slug,
content: page.content,
id: page._id
});
}
});
});
我在 mongo compass 中的数据结构的副本:
{
"_id":"5a9568fa5ce418423ca8e29f",
"title":"Home",
"slug":"home",
"content":"Home Page for Node CMS Cart",
"sorting":100,
"__v":0
}
抛出错误:
GET /admin/routes 200 8.634 ms - 3064
slug: about-us
null
events.js:183
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'title' of null
at Pages.findOne (E:\nodeCMSApp\routes\admin_routes.js:98:29)
at model.Query.<anonymous>
(E:\nodeCMSApp\node_modules\mongoose\lib\model.js:3928:16)
at E:\nodeCMSApp\node_modules\kareem\index.js:297:21
at E:\nodeCMSApp\node_modules\kareem\index.js:135:16
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
我似乎不明白为什么在我的回调中,我无法获得对 page.title 的引用,因为未指定时默认字段是所有字段。
PS:Pages 是我的模型类。
如果是,我该如何解决这个问题? 谢谢。
【问题讨论】:
-
你可以添加你正在调用的 URL。
-
你能告诉添加这些语句是什么打印的 console.log('slug:\t' + req.params.slug); console.log(page);
-
@Pavan,slug 打印正确,因为它来自 req 正文。但是,页面值为空
-
@RahulSharma,这是完整的 URL localhost:4003/admin/routes/edit-page/%20about-us。我不知道为什么要附加 %20,但这似乎不是问题
-
代码看起来很好,只有你必须处理条件。
标签: node.js mongodb express mongoose mongodb-query