android - MissingSchemaError:尚未为模型产品注册架构
问题描述
MissingSchemaError:尚未为模型产品注册架构 getCartItems MissingSchemaError:尚未为模型“产品”注册架构。在 NativeConnection.Connection.model (F:\ecommerceApp\ecommerce-backend\node_modules\mongoose\lib\connection.js:1242:11) 使用 mongoose.model(name, schema) 这是控制器
大车
const mongoose = require('mongoose');
const cartSchema = new mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: 'user', required: true },
cartItems: [
{
product : {
type :mongoose.Schema.Types.ObjectId,
ref : "product",
required:true
},
quantity : {
type:Number,
default:1
}
}
]
} ,{timestamps:true});
module.exports = mongoose.model('Cart' , cartSchema , 'cart');
产品
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
slug: {
type: String,
required: true,
unique: true
},
quantity: {
type: Number,
required: true
},
price: {
type: Number,
required: true
},
description: {
type: String,
required: true,
trim: true
},
offer: { type: Number },
productPictures: [{
img: { type: String }
}],
reviews: [{
userid: { type: mongoose.Schema.Types.ObjectId, ref: "User", review: String }
}],
category: { type: mongoose.Schema.Types.ObjectId, ref: "Categories" },
createdby: {
type: mongoose.Schema.Types.ObjectId, ref: "User", required: true
},
updatedAT: {
type: Date
}
}, { timestamps: true });
module.exports = mongoose.model('Product', productSchema);
文档
产品
{"_id":{"$oid":"6027d3a8cf712436607e6a6f"},"name":"Samsung Galaxy S21 Plus (Phantom Silver, 256 GB) (8 GB RAM)","slug":"Samsung-Galaxy-S21-Plus-(Phantom-Silver-256-GB)-(8-GB-RAM)","price":5000,"quantity":10,"description":"With the Samsung Galaxy S21 Plus Smartphone, you can click amazing shots and record stunning videos at up to 8K resolution as it comes with a 64 MP camera setup. This smartphone is 5G-ready, enabling you to stream videos and download content from the Web with minimal lag. Moreover, its powerful processor and long-lasting battery make it a delight to use this smartphone.","productPictures":[{"_id":{"$oid":"6027d3a8cf712436607e6a70"},"img":"wGFfBkAhQ-samsung-galaxy-j6-sm-j600gzbgins-original-imaf6zpf6q8tjcva.jpeg"}],"category":{"$oid":"6020d53117616c5228246ac4"},"createdby":{"$oid":"601400873e5a422f9c81ae1c"},"reviews":[],"createdAt":{"$date":"2021-02-13T13:27:04.135Z"},"updatedAt":{"$date":"2021-02-13T13:27:04.135Z"},"__v":0}
大车
{"_id":{"$oid":"6027d3bacf712436607e6a71"},"user":{"$oid":"60238bea8f89fb538c845f5b"},"cartItems":[{"quantity":2,"_id":{"$oid":"6027d3d4cf712436607e6a73"},"product":{"$oid":"6027d3a8cf712436607e6a6f"}}],"createdAt":{"$date":"2021-02-13T13:27:22.393Z"},"updatedAt":{"$date":"2021-02-13T13:27:48.217Z"},"__v":0}
路由器导出方法,这是未填充人口未发生的错误
被邮递员召唤时投掷
exports.getCartItems = (req, res) => {
//const { user } = req.body.payload;
//if(user){
console.log('req.user._id' , req.user._id);
Cart.findOne({ user: req.user._id })
.populate("cartItems.product", "_id name price productPictures")
.exec((error, cart) => {
console.log('getCartItems' , error);
if (error) return res.status(400).json({ error });
if (cart) {
let cartItems = {};
cart.cartItems.forEach((item, index) => {
cartItems[item.product._id.toString()] = {
_id: item.product._id.toString(),
name: item.product.name,
img: item.product.productPictures[0].img,
price: item.product.price,
qty: item.quantity,
};
});
res.status(200).json({ cartItems });
}
});
//}
};
解决方案
你有一个错字。你的意思是products
但输入proucts
推荐阅读
- python - 为什么我需要使用静态文件夹来显示带有flask的图像
- node.js - 如何从 get 方法中的 cors 禁用选项方法
- python - 使用 Symfit 进行全局拟合:数据集的类型结构
- kotlin - Kotlin-Native cinterop 从 C 更新 Struc 字段
- scala - AnalysisException:在数据架构中发现重复的列:`hour`、`eventTime`
- clickhouse - 将具有动态更改架构的事件数据流式传输到 ClickHouse
- laravel - laravel 无法访问关系属性
- python - Python多处理函数卡在一个带有while循环的函数中
- java - Android 照片上的 EXIF 方向字段为空
- angular - Angular 表单在使用 ng-zorro 初始化时未显示错误