簡體   English   中英

貓鼬將產品添加到購物車產品數組

[英]Mongoose adding product to Cart products array

我一直在試圖弄清楚如何push新產品push送到我數據庫中的現有cart

updateOne正在搜索具有聲明userId購物車,然后綁定以將新產品插入到 products 數組中。

這是應該如何使用$addToSet嗎? 任何引導我走向正確方向的提示和建議都非常感謝!

我也嘗試過其他方法,如插入、插入、更新,老實說,我不太確定在這種情況下使用哪一種。

編輯:上一條消息“購物車已經存在”只是我的 console.log,所以問題是什么都沒有發生,在 mongo 或 express 中都沒有,沒有錯誤消息,我很快就會查看“可能重復”的建議。

貓鼬購物車集合:

{
    "products": [{
        "_id": {
            "$oid": "5f948e5bd8615318f662d315"
        },
        "title": "BAND",
        "price": 16,
        "amount": 25
    }],
    "totalPrice": 416,
    "userId": {
        "$oid": "5f940f8876ad3e073a2e1e8b"
    },
    "__v": 0
}

購物車架構:

const mongoose = require('mongoose');

const cartSchema = new mongoose.Schema(
    {
        userId: {
            type: mongoose.Types.ObjectId,
            ref: "User"
        },
        products: [
            {
                title: String,
                price: Number,
                amount: Number
            }
        ],
        totalPrice: Number
    }
);

const CartModel = mongoose.model('CartModel', cartSchema, "carts");

module.exports = CartModel

購物車 :

let cart = null;
const CartModel = require('./cartModel')

module.exports = class Cart {

    static save(product) {

        let cartModel = new CartModel();

        console.log('Product: ' + product)
        if (cart == null) {
            cart = {products: [], totalPrice: 0}
        }

        cartModel.updateOne(
            {
                userId: '5f940f8876ad3e073a2e1e8b'
            },
            {
                $addToSet: {
                    products: [
                        {
                            'title': product.title,
                            'price': product.price,
                            'amount': product.amount
                        }
                    ],
                    totalPrice: product.amount * product.price
                }
            },
            {upsert: true, new: true},
            {multi: true},
            function (err, res) {
                console.log(err, res)
            });
    }

編輯:解決方案在: https : //stackoverflow.com/a/46995621/11004824 “解決方案是在模型上運行函數,而不是在它的實例上運行”我不知道在模型實例上運行不會產生好的結果。 我的工作代碼

let cart = null;
const CartModel = require('./cartModel')

module.exports = class Cart {

    static save(product) {

        console.log('Product: ' + product)
        if (cart == null) {
            cart = {products: [], totalPrice: 0}
        }

        let productObj = {
            'title': product.title,
            'price': product.price,
            'amount': product.amount
        };

        CartModel.update(
            {
                userId: '5f940f8876ad3e073a2e1e8b'
            },
            {
                $push: {
                    products: productObj
                },
                $set: {
                    totalPrice: product.amount * product.price
                }
            },
            {upsert: true, new: true},
            function (error, success) {
                if (error) {
                    console.log(error)
                } else {
                    console.log(success)
                }
            }
        );
    }

$addToSet在數組中推送一個值(如果它不存在)。 您要做的是使用 $addToSet 更新購物車。 問題是,你也在努力更新totalPrice使用addToSet 看看這個Mongo Playground

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM