根据变化反应猫鼬商品价格

React Mongoose item price based on variations

我正在尝试应用主题中使用的方法根据所选标准管理同一商品的不同价格: MongoDB Schema for ecommerce products that have variations with different SKUs, prices and stock

但是关于这个方案:

dimensions: {
  color: {
    title: { type: String },
    red: {
      title: { type: String },
      images: [{ type: String }],
    },
  },
  size: {
    title: { type: String },
    s05: {
      title: { type: String },
      images: [{ type: String }],
    },
  },
  variations: [
    {
      dimensions: {
        color: { type: String },
        size: { type: String },
        fireretardant: { type: String },
      },
      SKU: { type: String },
      price: { type: Number },
    },
  ],
},

当我尝试创建对象时,出现此错误:

file:///C:/Users/ReactProject/shop-v2/backend/data.js:68 dimensions: { color: red, size: s1, fireretardant: no }, ReferenceError: red is not defined

如何让 Mongoose 考虑定义的红色和大小?

dimensions: {
        color: {
          title: "Color",
          red: {
            title: "Red",
            images: [
              "http://myserver.com/images/product_12345_3",
              "http://myserver.com/images/product_12345_4",
            ],
          },
        },
        size: {
          title: "Size",
          s05: {
            title: "0.5 m",
            images: [],
          },
        },
        variations: [
          {
            dimensions: { color: red, size: s1 },
            SKU: "98765",
            price: 10,
          },
        ],
      },

问题出在这一行

dimensions: { color: red, size: s1 }

颜色和大小设置为变量。您的意思是将它们作为字符串输入吗?如果是这样,请尝试:

dimensions: { color: "red", size: "s1" }

如果它们是变量,则检查这些变量是否已定义,并且它们在给定函数调用的范围内可用。

错误的意思是它正在寻找名为red的变量,但没有找到。