【问题标题】:How to create an order from a cart in Mongoose and delete cart如何在 Mongoose 中从购物车创建订单并删除购物车
【发布时间】:2021-04-22 14:05:18
【问题描述】:

我正在尝试: a) 从我当前的购物车创建一个订单 b) 使用“选择”中的选定对象属性填充订单的响应 c) 最后,我想删除购物车。

我已经尝试了以下方法,但这是我的问题:

  1. 即使确实填充了 prevProductsQuantity 常量,结果响应也不会返回填充
  2. 我创建了函数 cartClear 来接收购物车 ID 并从模型中删除文档,但它不起作用。
  3. 我能够创建许多“相同”的订单,所以我认为我需要在有条件的情况下进行管理,但不确定在哪里。

我正在寻找的回应:

{
"success": true,
"data": {
  "_id": "607ed5c425ae063f7469a807",
  "userId": "6071aed0ed7ec9344cf9616c",
  "productsQuantity": [
    {
      "_id": "607f2507994d5e4bf4d91879",
      "productId": {
        "productRef": "463b8bb7-6cf6-4a97-a665-ab5730b69ba2",
        "productName": "table",
        "brand": "boehm llc",
        "price": 713,
        "__v": 0,
        "createdAt": "2021-04-09T18:31:43.430Z",
        "updatedAt": "2021-04-09T18:31:43.430Z"
      },
      "quantity": 2,
      "updatedAt": "2021-04-21T15:12:51.894Z",
      "createdAt": "2021-04-21T15:12:51.894Z"
    }
  ],
  "__v": 0
}

++删除购物车++

我得到的当前响应:

{
  "success": true,
  "data": {
    "state": "pending-payment",
    "_id": "6080507a0c758608d0dc585c",
    "userId": "6071aed0ed7ec9344cf9616c",
    "totalPrice": 1426,
    "productsQuantity": [
      {
        "_id": "607f2507994d5e4bf4d91879",
        "productId": "60709d8f24a9615d9cff2b69",
        "quantity": 2,
        "updatedAt": "2021-04-21T15:12:51.894Z",
        "createdAt": "2021-04-21T15:12:51.894Z"
      }
    ],
  "createdAt": "2021-04-21T16:19:06.056Z",
  "updatedAt": "2021-04-21T16:19:06.056Z",
  "__v": 0
}

** 并且购物车没有删除 **

这是我为这些目的输入的代码。

非常感谢任何指导

router.post('/', [isAuthenticated], async (req, res, next) => {
  try {
    const cart = await CartModel.findOne({ userId: req.user }).populate({
      path: 'productsQuantity.productId',
      select: {
        price: 1,
        brand: 1,
        productName: 1,
        productRef: 1,
        pictures: 1
      }
    });

    const prevProductsQuantity = cart
      .get('productsQuantity')
      .map((el) => el.toObject());

    const totalPriceByProduct = prevProductsQuantity.map(
      (product) => product.productId.price * product.quantity
    );
    const totalPrice = totalPriceByProduct.reduce(function (a, b) {
      return a + b;
    });

    const result = await OrderModel.create({
      userId: req.user,
      totalPrice: totalPrice,
      state: 'pending-payment',
      productsQuantity: prevProductsQuantity
    });

    const cartClear = (id) =>
      CartModel.deleteOne({
        _id: id._id
      });

    res.status(200).json({
      success: true,
      data: result
    });
    cartClear(`${cart._id.toString()}`);
  } catch (error) {
    next(error);
  }
});

【问题讨论】:

    标签: javascript mongodb express mongoose mongoose-schema


    【解决方案1】:

    您没有解决 deleteOne 承诺,缺少异步和等待。

    const cartClear = async (id) =>
      CartModel.deleteOne({
        _id: id
      });
    
     await cartClear(`${cart._id.toString()}`)
     res.status(200).json({
       success: true,
       data: result
     });
    

    【讨论】:

    • 你是对的,我没有正确处理承诺。我使用了这个并且它有效:CartModel.findByIdAndDelete(cartId, function (err) { if (err) console.log(err); console. log('删除成功'); }); // 谢谢提示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2013-01-11
    • 2016-01-29
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多