尝试在 MongoDB 数据模型中存储一些数据时出现问题

Posted

技术标签:

【中文标题】尝试在 MongoDB 数据模型中存储一些数据时出现问题【英文标题】:Issue when trying to store some data in a MongoDB data model 【发布时间】: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@ 为必填项。”

【问题讨论】:

【参考方案1】:

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

【讨论】:

非常感谢,内纳德·米洛萨夫列维奇

以上是关于尝试在 MongoDB 数据模型中存储一些数据时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用 MongoDB 从解析服务器中保存或检索数据时出现“服务器在没有 SSL 支持的情况下启动”错误

将带有数字键的数据插入 mongodb 时出错(python 字典中的 GPS 数据)

IDE 在导入 mongodb 存储库时出现错误

如何在 mongoose 或 mongodb 中制作数据模型

在 SQL 数据建模器中将逻辑模型转换为关系模型时出现问题

MongoDB数据模型和索引学习总结