集合FindById()不起作用。猫鼬

时间:2017-11-29 10:16:05

标签: node.js mongoose

这是我的特定路线的节点js代码。 `

        DiscountCoupon.findById(req.body._id,function (err, data) {

            data.coupon_code = req.body.coupon_code;
            data.status = req.body.status;
            data.max_redemption = req.body.max_redemption;
            data.expiry_date = req.body.expiry_date;
            data.valid_from = req.body.valid_from;

            data.discount_value = req.body.discount_value;
            data.is_percentage = req.body.is_percentage;
            data.max_discount_amount = req.body.max_discount_amount;
            data.save(function (err, regDoc) {
                if (err)
                    return next(err);
                if (!regDoc)
                    return next(new Error('not found'));

                return res.status(200).send({ 'message': 'Record updated.' });
            });
        });

`

{   "max_discount_amount": 50,
    "is_percentage": false,
    "discount_value": 50,
    "valid_from": "2017-11-25T00:00:00.000Z",
    "expiry_date": "2017-12-15T00:00:00.000Z",
    "max_redemption": 100,
    "coupon_code": "CODE50",
    "_id": "5a12eb396b6fc23f7c2c63cc",
    "created_date": "2017-11-20T14:48:08.559Z",
    "status": true }

我没有收到任何错误消息或任何结果。 谁能解决我的问题?

1 个答案:

答案 0 :(得分:1)

更改您的代码,如下所示

DiscountCoupon.findById(req.body._id,function (err, data) {
            if(err)
               return next(err);

            data.coupon_code = req.body.coupon_code;
            data.status = req.body.status;
            data.max_redemption = req.body.max_redemption;
            data.expiry_date = req.body.expiry_date;
            data.valid_from = req.body.valid_from;

            data.discount_value = req.body.discount_value;
            data.is_percentage = req.body.is_percentage;
            data.max_discount_amount = req.body.max_discount_amount;
            data.save(function (err1, regDoc) {
                if (err1)
                    return next(err1);
                if (!regDoc)
                    return next(new Error('not found'));

                return res.status(200).send({ 'message': 'Record updated.' });
            });
        });