Javascript MongoDB中显示单据字段时出现问题

Javascript MongoDB中显示单据字段时出现问题,javascript,mongodb,express,mongoose,mongodb-query,Javascript,Mongodb,Express,Mongoose,Mongodb Query,我刚开始编程,并尽了最大努力,但没有任何帮助,我认为我无法找到它;) 我想按品牌展示mongoDB的所有产品。我已经有了分类的路线,但与此同时,当我试图对品牌实施相同的策略时,我没有找到404。 我的代码: router.get('/:categorySlug', (req, res, next) => { let filter = {}; if(req.query.hasOwnProperty("filter")){ filter['price'] = req.query.pr

我刚开始编程,并尽了最大努力,但没有任何帮助,我认为我无法找到它;) 我想按品牌展示mongoDB的所有产品。我已经有了分类的路线,但与此同时,当我试图对品牌实施相同的策略时,我没有找到404。 我的代码:

router.get('/:categorySlug', (req, res, next) => {

let filter = {};
if(req.query.hasOwnProperty("filter")){
    filter['price'] = req.query.price
}

const slug = req.params.categorySlug;
Category.findOne({slug: slug})
.select('_id')
.exec()
.then(category => {
    if(category){
        if(category.parent === ""){
            Product.find({category: category._id})
            .select('_id name price productPic category brand slug')
            .sort(filter)
            .exec()
            .then(products => {
                res.status(200).json({
                    message: products
                })
            })
            .catch(error => {
                return res.status(404).json({
                    message: error
                })
            })
        }
    }else{
        return res.status(404).json({
            message: 'Not Found'
        })
    }
})
.catch(er => {
    res.status(500).json({
        error: er
    });
});



router.get('/:brandSlug', (req, res, next) => {

const slug = req.params.brandSlug;
Brand.findOne({slug: slug})
.select('_id parent')
.exec()
.then(brand => {
    if(brand){
            Product.find({brand: brand._id})
            .select('_id name price productPic brand slug')
            .exec()
            .then(products => {
                res.status(200).json({
                    message: products
                })
            })
            .catch(error => {
                return res.status(404).json({
                    message: error
                })
            })
    }else{
        return res.status(404).json({
            message: 'Not Found'
        })
    }
})
.catch(er => {
    res.status(500).json({
        error: er
    });
});
类别、品牌和产品模式:

const brandSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
slug: { type: String, required: true, unique: true },


const categorySchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
slug: { type: String, unique: true },


const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
slug: { type: String, required: true, unique: true },
price: { type: Number, required: true },
oldPrice: { type: Number, default: 0 },
stock: { type: Number, required: true },
description: { type: String },
productPic: [
    {
        img: String
    }
],
keyword: {type: String},
category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true  },
brand: { type: mongoose.Schema.Types.ObjectId, ref: 'Brand', required: true }

您不需要执行两个DB调用来获取所需数据,相反,您可以将这两个集合合并并在一个DB调用中获取所需数据(请尝试阅读MongoDB的本机或Mongoose,这是
$lookup
的一种包装)

但现在你可以替换这个:

Product.find({brand: brand._id})
与:

Product.find({brand: mongoose.Types.ObjectId(brand._id)})
实际问题是
brand.\u id
是代码中的字符串(通常,当您在MongoDB中阅读
brand
集合的文档,然后记录/打印它,或者甚至在代码中使用它时,您可以看到
ObjectId()
将其转换为字符串作为
ObjectId())
来自BSON&不受JSON支持。因此,您需要将输入(
brand.\u id
)从字符串转换为
ObjectId()
,然后再触发对产品集合的查询,因为产品集合中的品牌字段的类型为
ObjectId()

注意:别忘了在这个文件中导入猫鼬,否则它会失败


您不需要执行两个DB调用来获取所需数据,相反,您可以将这两个集合合并并在一个DB调用中获取所需数据(请尝试阅读MongoDB的本机或Mongoose,这是
$lookup
的一种包装)

但现在你可以替换这个:

Product.find({brand: brand._id})
与:

Product.find({brand: mongoose.Types.ObjectId(brand._id)})
实际问题是
brand.\u id
是代码中的字符串(通常,当您在MongoDB中阅读
brand
集合的文档,然后记录/打印它,或者甚至在代码中使用它时,您可以看到
ObjectId()
将其转换为字符串作为
ObjectId())
来自BSON&不受JSON支持。因此,您需要将输入(
brand.\u id
)从字符串转换为
ObjectId()
,然后再触发对产品集合的查询,因为产品集合中的品牌字段的类型为
ObjectId()

注意:别忘了在这个文件中导入猫鼬,否则它会失败


你能告诉我你想在前端买到什么牌子的代码吗?没有前端。只是现在就构建一个api,同时从邮递员处为/products/:categorySlug发送GET req,这是一个可行的方法,但对于品牌来说,这不是现在的想法why@HubertKopyść:我的回答解释了部分代码中缺少的内容,不管怎样,是为了检查您为什么没有获得品牌(在这里:
if(brand)
),您是否可以尝试打印此
req.params.brandSlug
,并直接在MongoDB中执行相同的
.findOne()
查询,并检查是否有任何文档返回该输入?能否在前端显示您尝试获取品牌的代码?没有前端。只是现在就构建一个api,同时从邮递员处为/products/:categorySlug发送GET req,这是一个可行的方法,但对于品牌来说,这不是现在的想法why@HubertKopyść:我的回答解释了部分代码中缺少的内容,不管怎样,是为了检查您为什么没有获得品牌(在这里:
if(brand)
),您是否可以尝试打印此
req.params.brandSlug
,并直接在MongoDB中执行相同的
.findOne()
查询,并检查是否返回了该输入的任何文档?