我无法在购物车路由器中填充该物品。该项目是ItemModel的实例

我想填充购物车路线中的项目,但无法填充。我无法在购物车路由器中填充该物品。该项目是ItemModel的实例。

ItemModel.js

const mongoose = require('mongoose');

const ItemSchema = new mongoose.Schema({
    subtaskItemName: {
        type: String,
        required: true,
        unique: true,
        trim: true
    },
    subtaskItemImage: {
        type: String
    },
    subtaskItemTime: {
        type: String
    },
    description: {
        type: String,
        trim: true
    },
    subtaskItemPrice: {
        type: String
    },
    subtaskItemStatus: {
        type: Boolean,
        default: true
    },
    subtask: {
        type: mongoose.Schema.ObjectId,
        ref: 'SubTask',
        required: true
    },
    createdAT: {
        type: Date,
        default: Date.now
    }

})

module.exports = mongoose.model("Item", ItemSchema);

CartModel.js

const monngose = require('mongoose');

const Schema = monngose.Schema;

const CartSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'user'
    },
    task_items: [{
        task_item: {type:Schema.Types.ObjectId, ref: 'SubTaskCategoryItem'},
        quantity: { type: Number, default: 1 }

    }],
    totalPrice: {
        type: Number
    },
    item: {
        type: Schema.Types.ObjectId, 
        ref: 'Item'
    },
    crreated: {
        type: Date,
        default: Date.now
    }
});
module.exports = monngose.model('Cart', CartSchema);

CartRoute.js

const express = require('express');
const Cart = require('../../models/admin/CartModel');
const checkJwt = require('../../middleware/admin/user_auth');
const TaskItem = require('../../models/admin/ItemModel');

const router = express.Router();
  router.get('/get_cart', checkJwt, (req, res, next) => {

    Cart.find({ user:  req.user.id })
      .populate({
        path: 'user'
      })
      .populate('item')
    .exec((err, cart) => {
      res.json({
        msg: 'User not exists',
        cart: cart
      });

    })
  });

  module.exports = router;

如何在cart.js路由器中填充项目字段。我想填充购物车路线中的物品,但无法填充。我无法在购物车路由器中填充该物品。该项目是ItemModel的实例

评论
  • HM
    HM 回复

    我将任务项数组下的任务项(如果有项目ID)。

      {
            "msg": "User not exists",
            "cart": [
                {
                    "_id": "5ec0c28cf494113aa06c99d9",
                    "task_items": [
                        {
                            "quantity": 1,
                            "_id": "5ec0c28cf494113aa06c99da",
                            "task_item": "5ebe499d7e657d322c39345f"
                        },
                        {
                            "quantity": 1,
                            "_id": "5ec0d018ecc2411cf40bceae",
                            "task_item": "5ebe499d7e657d322c39345f"
                        },
                        {
                            "quantity": 1,
                            "_id": "5ec0d4abfac25246689d15a7",
                            "task_item": "5ec0d4a2fac25246689d15a6"
                        }
                    ],
                    "crreated": "2020-05-17T04:50:20.869Z",
                    "user": {
                        "_id": "5ebe4d5d440ab504c8a0d128",
                        "name": "Ajit",
                        "phone": "9668854799",
                        "avatar": "//www.gravatar.com/avatar/20c3d59b7049c9ad85cd33e2ebd3151c?s=200&r=pg&d=mm",
                        "password": "$2a$10$FdH7QzoqOHnVoLbbm9GaUOLaJbTTqPWzFWeFACkea2NWlrJrFxZ5a",
                        "createdAt": "2020-05-15T08:05:49.418Z",
                        "__v": 0
                    },
                    "__v": 2
                }
            ]
        }