首页 > 解决方案 > 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 });
      }
    });
  //}
};

标签: androidnode.jsreactjsmongodbmongoose

解决方案


你有一个错字。你的意思是products但输入proucts


推荐阅读