【问题标题】:Issue when trying to store some data in a MongoDB data model尝试在 MongoDB 数据模型中存储一些数据时出现问题
【发布时间】:2021-06-30 08:23:45
【问题描述】:

我想在以下 MongoDB 模型中存储一些数据:

import mongoose from 'mongoose';

const orderSchema = mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: 'User',
    },
    orderItems: [
      {
        name: { type: String, required: true },
        qty: { type: Number, required: true },
        image: { type: String, required: true },
        price: { type: Number, required: true },
        product: {
          type: mongoose.Schema.Types.ObjectId,
          required: true,
          ref: 'Product',
        },
      },
    ],
    shippingAdress: {
      address: { type: String, required: true },
      city: { type: String, required: true },
      postalCode: { type: String, required: true },
      state: { type: String, required: false },
      country: { type: String, required: true },
    },
    paymentMethod: {
      type: String,
      required: true,
    },
    paymentResult: {
      id: { type: String },
      status: { type: String },
      update_time: { type: String },
      email_address: { type: String },
    },
    taxPrice: {
      type: Number,
      required: true,
      default: 0.0,
    },
    shippingPrice: {
      type: Number,
      required: true,
      default: 0.0,
    },
    totalPrice: {
      type: Number,
      required: true,
      default: 0.0,
    },
    isPaid: {
      type: Boolean,
      required: true,
      default: false,
    },
    paidAt: {
      type: Date,
    },
    isDelivered: {
      type: Boolean,
      required: true,
      default: false,
    },
    deliveredAt: {
      type: Date,
    },
  },
  { timestamps: true }
);

const Order = mongoose.model('Order', orderSchema);

export default Order;

但我无法将以下示例数据发布到数据库中

{
 "orderItems": [
     {
       "product": "6060f2eb6bea3f2280c08a4c",
       "name": "Airpods Wireless Bluetooth Headphones",
       "image": "/images/airpods.jpg",
       "price": 89.99,
       "qty": 3
     }
   ],
   "user": "6060f2eb6bea3f2280c08a4b",
   "shippingAddress": {
     "address": "1st Avenue Main St",
     "city": "Boston",
     "postalCode": "02011",
     "country": "USA"
   },
   "paymentMethod": "Stripe",
   "itemsPrice": 269.97,
   "taxPrice": 40.50,
   "shippingPrice": 20.00,
   "totalPrice": 330.47
}

当我收到以下错误时: 顺便说一句,我正试图通过 Postman 完成这项工作

"订单验证失败:shippingAdress.country: Path shippingAdress.country 是必填项,shippingAdress.postalCode: Path shippingAdress.postalCode 是必填项。 shippingAdress.city: Path shippingAdress.city 是必填项。 shippingAdress.address: 路径@ 987654326@ 为必填项。”

【问题讨论】:

    标签: node.js json mongodb rest mongoose


    【解决方案1】:

    您的模型shippingAdress 中有错字。应该是shippingAddress

    【讨论】:

    • 非常感谢,内纳德·米洛萨夫列维奇
    猜你喜欢
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多