【问题标题】:How to increase quantity without adding the same object again?如何在不再次添加相同对象的情况下增加数量?
【发布时间】:2021-12-05 21:48:54
【问题描述】:

我正在尝试使用 node 为我的购物车构建后端。如果产品 ID 相同,我想为购物车创建 API 以添加产品并增加数量,但不知道如何执行此操作。 提前感谢大家帮助我。

这是我的模型:

module.exports = (mongoose) =>
    new mongoose.Schema(
    {
      userId: { type: Number, required: true, unique: true },
      product: {
        productId: { type: Number, required: true },
        slug: { type: String, required: true },
        name: { type: String, required: true },
        image: { type: String, required: true },
      },
      options: [
        {
          optionId: { type: Number, required: true },
          optionTitle: { type: String, required: true },
          valueId: { type: Number, required: true },
          valueTitle: { type: String, required: true },
        },
      ],
      price: { type: Number, required: true },
      quantity: { type: Number, required: true, min: 1 },
    },
    { timestamps: true }
  );

这是我的服务方法: 在这种方法中,我可以做些什么来增加数量而不是再次添加相同的产品?

const db = require('../models');
const logger = require('../utils/logger');

const service = 'cart';
const tag = service + '.js';

module.exports = {
    addItem: async (payload, auth) => {
    try {
      const cartExist = await db.Cart.findOne({
        userId: auth.credentials.customerId,
      });
      if (!cartExist) {
        const cart = {
          userId: auth.credentials.customerId,
          product: payload.product,
          options: payload.options,
          price: payload.price,
          quantity: payload.quantity,
        };
        await db.Cart.create(cart);
        return { success: true, data: cart };
      } else {
        console.log('Quantity increased');
      }
    } catch (error) {
      logger.error(tag + ': add', error);

      return { success: false, data: error };
    }
  },
};

【问题讨论】:

    标签: node.js mongodb mongoose hapi


    【解决方案1】:

    我建议您更改购物车Schema 的设计方式。 每个购物车都可以有许多产品,因此最好将产品信息存储在 array 中。每个产品都可以有一个quantity 密钥。所以你的 Schema 应该是这样的:

    module.exports = (mongoose) =>
    new mongoose.Schema(
    {
      userId: { type: Number, required: true, unique: true },
      products: [{
        productId: { type: Number, required: true },
        slug: { type: String, required: true },
        name: { type: String, required: true },
        image: { type: String, required: true },
        quantity : {type : Number , required : true, default : 1 , min : 1}
      }],
      options: [  
        {
          optionId: { type: Number, required: true },
          optionTitle: { type: String, required: true },
          valueId: { type: Number, required: true },
          valueTitle: { type: String, required: true },
        },
      ],
      price: { type: Number, required: true },
    },
    { timestamps: true }
    );
    

    在服务方法中,您可以检查payload.product._id是否已经在购物车的产品中,如果是,则只需更改该特定产品的数量即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 2017-02-01
      相关资源
      最近更新 更多