@aleshaykovlev
html, css, js, node, webpack, sass, react

Не пушится в массив, что не так?

Нужно добавить размеры в объект корзины. Нашел в инете такой способ, но почему то не работает, корзина не обновляется.

POST запрос:
router.post('/add', auth, async(req, res) => {
    try {
        const product = await Product.findById(
            req.body.id, req.body.title, req.body.nowPrice, req.body.mainSize,
            req.body.article, req.body.sex
        );

        product.size = req.body.size;

        mongoose.connection.db.collection('products', (err, docs) => {
            Product.findOne((err, result) => {
                if (err) return console.log(err);

                if (result) {
                    Product.update({
                        '$push': {
                            'cart': {
                                'items': {
                                    '$each': product,
                                    'size': {
                                        '$each': product.size
                                    }
                                }
                            }
                        }
                    })

                    res.redirect('/cart');
                } else {
                    res.status(400).send({ message: 'Произошла ошибка' });
                }
            });
        });
    } catch (e) {
        res.status(400).send({ message: 'Произошла ошибка ' + e.message });
    }
})


Модель пользователя:
const user = new Schema({
    firstName: String,
    lastName: String,
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    cart: {
        items: [{
            article: {
                type: String,
                required: true
            },
            title: {
                type: String,
                required: true
            },
            count: {
                type: Number,
                required: true,
                default: 1
            },
            price: {
                type: Number,
                required: true
            },
            mainSize: {
                type: String
            },
            size: [{
                type: String
            }],
            productId: {
                type: Schema.Types.ObjectId,
                ref: 'Product',
                required: true
            }
        }]
    }
})
  • Вопрос задан
  • 64 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы