【问题标题】:find() blogs by category in mongoose not working with NodeJs在猫鼬中按类别查找()博客不使用 NodeJs
【发布时间】: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"
        }

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    如果你得到结果然后访问 categoryId,你应该这样做:

    result.data[0].categoryId 
    //this will give you first object in the array.
    

    【讨论】:

    • 如果我通过 result.data[0].categoryId 来访问 categoryId,它的返回无法读取未定义的属性 '0'
    • 只需将 result.data 打印到控制台并查看您正在获取 json 对象或 json 数组,如果您正在获取 json 对象,请尝试 result.data.categoryId
    猜你喜欢
    • 2020-10-19
    • 2017-04-06
    • 2016-01-07
    • 2016-10-13
    • 2018-08-21
    • 2020-03-06
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多