【问题标题】:React Mongoose item price based on variations根据变化反应 Mongoose 项目价格
【发布时间】:2021-12-28 01:08:38
【问题描述】:

我正在尝试应用主题中使用的方法来根据所选标准管理同一商品的不同价格: 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 尺寸:{颜色:红色,尺寸:s1,阻燃:无}, ReferenceError:红色未定义

如何让 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,
          },
        ],
      },

【问题讨论】:

    标签: javascript reactjs mongoose variations


    【解决方案1】:

    问题出在这一行

    dimensions: { color: red, size: s1 }
    

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

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

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

    错误表示它正在寻找名为red的变量,但没有找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      相关资源
      最近更新 更多