首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在猫鼬v6中获取查询种群中的错误

在猫鼬v6中获取查询种群中的错误
EN

Stack Overflow用户
提问于 2022-05-17 09:11:43
回答 1查看 32关注 0票数 0

我想填充产品架构中的类别字段。

我的产品架构的代码是

代码语言:javascript
复制
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);

用于填充类别字段的代码是

代码语言:javascript
复制
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以覆盖。

我读过文档,但无法更改代码。请帮我解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2022-05-17 09:35:07

检查产品数据库表中是否有类别键,并且您应该知道对象Id是正确的,并且它来自类别表,如果不是,您应该删除数据库或删除产品集合并创建新的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72271269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档