【发布时间】:2020-05-30 14:05:42
【问题描述】:
我正在使用 mongoose 框架、带有 express 和 angular 的 nodeJs 制作一个带有 mongodb 数据库的博客应用程序。现在,我正在尝试创建一个组件,其中从类别架构中获取所有博客类别,然后单击它们,在其类别字段中具有该 categoryId 的博客架构上的所有博客都应该出现。当检查 db as db.blogs.find({'categoryId': 'TRxtZfez'}) 所有具有此特定 categoryId 的博客都很好地显示出来。但是在检查节点控制器函数 result.categoryId 时正在获取 undefined
节点功能:
app.get(baseUrl+'view/by/category/:categoryId', (req, res) => {
BlogModel.find({ 'categoryId': req.params.categoryId }, (err, result) => {
if (err) {console.log(err)
let apiResponse = response.generate(true, 'Failed to find category details', 500, null)
res.send(apiResponse)
} else if (result == undefined || result == null || result == '') {
console.log('No Blog Found')
let apiResponse = response.generate(true, 'No such category Found', 404, null)
res.send(apiResponse)
} else { let apiResponse = response.generate(false, 'All Blogs with category :: found Successfully', 200, result)
console.log('BBB'+result.categoryId) //getting undefined
res.send(apiResponse)}})})
我的博客数据库的外观:
{
"error": false,
"message": "All Blogs found Successfully",
"status": 200,
"data": [
{
"title": "MongoDB",
"description": "MongoDB is a cross-platform document-oriented database program.",
"imagePath": "uploads\\1581692172205slider2.png",
"blogId": "K0llE03j",
"categoryName": "Technology",
"categoryId": "TRxtZfez"
},
{
"title": "NodeJs",
"imagePath": "uploads\\1581692310094shutterstock_339911378-350x350[1].jpg",
"blogId": "zHi_CUpN",
"categoryName": "Technology",
"categoryId": "TRxtZfez"
},
{
"title": "Angular",
"description": "Angular is a platform for building mobile and desktop web applications.",
"imagePath": "uploads\\1581692361330mp,550x550,gloss,ffffff,t.3u1[1].jpg",
"blogId": "ft4w9LVY",
"categoryName": "Technology",
"categoryId": "TRxtZfez"
}
类别数据库:
"categoryName": "Sports",
"categoryId": "rtBg1LCZ"
},
{
"categoryName": "Movies",
"categoryId": "zihrNEOP"
},
{
"categoryName": "Technology",
"categoryId": "TRxtZfez"
}
【问题讨论】: