【问题标题】:React mongoose validation failedReact mongoose 验证失败
【发布时间】:2021-09-06 13:28:33
【问题描述】:

我正在尝试存储订单数据,但将其添加到 mongoose 时出现以下错误:

请求:

回应:

这是我的源代码:

placeOrder 处理程序:

const placeOrderHandler = () => {
    dispatch(
      createOrder({
        orderItems: cartItems,
        shippingAddress: {
          address: shippingAddress.address,
          city: shippingAddress.city,
          postalCode: shippingAddress.postalCode,
          country: shippingAddress.country,
        },
        paymentMethod: paymentMethod,
        itemPrice: cart.itemPrice,
        shippingPrice: cart.shippingPrice,
        taxPrice: cart.taxPrice,
        totalPrice: cart.totalPrice,
      })
    );
  };

orderModel.js

const mongoose = require("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',
        },
      },
    ],
    shippingAddress: {
      address: { type: String, required: true },
      city: { type: String, required: true },
      postalCode: { type: String, required: true },
      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);

module.exports = Order;

如果我没记错的话,它会在 orderItems: 上抛出错误,但我正在发送 对象数组 数据,这些数据我已经在 orderModel 中定义了。 js.

【问题讨论】:

  • Yow items schema 有一个 product 属性,这是必需的,但是如果您查看客户端发送的 items 没有 product 属性,则有一个名为 productID 和 product !== productID

标签: node.js reactjs mongodb express mongoose


【解决方案1】:

正如@Ernesto 所说,我在我的架构中写的是product,而不是productID。这是正确的方法:

orderItems: [
      {
        name: { type: String, required: true },
        qty: { type: Number, required: true },
        image: { type: String, required: true },
        price: { type: Number, required: true },
        productID: {
          type: mongoose.Schema.Types.ObjectId,
          required: true,
          ref: 'Product',
        },
      },
    ],

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 2020-05-15
    • 2020-08-20
    • 1970-01-01
    • 2018-06-19
    • 2018-01-22
    • 2023-04-04
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多