我想填充产品架构中的类别字段。
我的产品架构的代码是
const mongoose = require('mongoose');
const {ObjectId} = mongoose.Schema
const productSchema = new mongoose.Schema({
name :{
type:String,
required : true,
trim : true,
maxlength : 32,
},
description : {
type:String,
required : true,
trim : true,
maxlength : 2000,
},
price : {
type : Number,
required : true,
maxlength :32
},
category : {
type : ObjectId,
ref : "Category",
required : true
},
stock : {
type : Number,
},
sold : {
type :Number,
default : 0
},
photo : {
data : Buffer,
contentType : String
}
},{timestamps:true})
module.exports = mongoose.model("Product",productSchema);用于填充类别字段的代码是
exports.getAllProducts = (req,res)=>{
let no_of_products = req.query.limit? parseInt(req.query.limit):8;
let sortBy = req.query.sortBy?req.query.sortBy : "price"
Product.find()
.populate("catgeory")
.select("-photo")
.sort({sortBy:1})
.limit(no_of_products)
.exec((err,products)=>{
if(err){
console.log(err);
return res.status(400).json({
error : "some error occured"
})
}
})
}但是这个代码给了我错误,上面写着
无法填充路径Catgeory,因为它不在架构中。将strictPopulate选项设置为false以覆盖。
我读过文档,但无法更改代码。请帮我解决这个问题。
发布于 2022-05-17 09:35:07
检查产品数据库表中是否有类别键,并且您应该知道对象Id是正确的,并且它来自类别表,如果不是,您应该删除数据库或删除产品集合并创建新的
https://stackoverflow.com/questions/72271269
复制相似问题