{
cartItems: [
{
product: { type: mongoose.Schema.Types.ObjectId, ref: "product" },
quantity: Number,
color: String,
price: Number,
},
],
totalPrice: Number,
totalPriceAfterDiscount: Number,
user: {
type: mongoose.Schema.ObjectId,
ref: "user",
},
},
{ timestamps: true }
);
// @desc query mongoose middleware to populate
CartSchema.pre(/find/, function (next) {
this.populate("user");
next();
});```
it throw this error
MissingSchemaError:尚未为模型“用户”注册架构。 使用 mongoose.model(名称,模式) 在 Connection.model (/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/connection.js:1236:13) 在 _getModelFromConn (/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:581:15) 在addModelNamesToMap(/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:507:17) 在 getModelsMapForPopulate (/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:198:7) 在填充(/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/model.js:4679:21) 在 _populate (/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/model.js:4639:5) 在/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/model.js:4616:5 在promiseOrCallback(/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/helpers/promiseOrCallback.js:11:14) 在 Mongoose._promiseOrCallback (/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/index.js:1266:10) 在 Model.populate (/home/melewasy/projects/full-E-commerce/node_modules/mongoose/lib/model.js:4614:23)"
i tried this line instead of mongoose middleware
let x = await Cart.findById(cart._id).populate("user");
导入用户架构
const User = require("file_path")
并将其添加到填充方法中
let x = await Cart.findById(cart._id).populate("user", User);
你修好了吗?我有同样的错误