Express-发送标头后无法设置标头

时间:2018-10-23 21:21:14

标签: node.js express

我知道这是一个经常被问到的问题,但无法弄清楚这里出了什么问题:

 router.post('/garage/cart',
    app.requirePermission([
        ['allow', {
            users: '@'
        }],
        ['deny', {
            users: '*'
        }]
    ]),
    function(req, res){
        var body = req.body;
        if(body.vehicleId 
            && body.garageId 
            && body.prestationId 
            && body.minPrice
            && body.maxPrice)
        {
            app.services.garage.createVirtualCart(body.vehicleId, body.garageId, function(cartId){
                var item = {
                    quote : {
                        quoteType : "ONLINE",
                        carServiceId : body.prestationId,
                        price : {
                            minPriceExclTax : parseFloat(body.minPrice),
                            maxPriceExclTax : parseFloat(body.maxPrice),
                        }
                    },
                    quantity : 1
                };

                app.services.garage.addItemsToCart(cartId, item, function(){
                    var partnerCart = app.models.PartnerCart.build();
                    partnerCart.cart_object = JSON.stringify(item);
                    partnerCart.cart_createdate = new Date();
                    partnerCart.cart_accountid = req.account.act_id;
                    partnerCart.cart_carid = body.vehicleId;
                    partnerCart.save();

                    return res.json({
                        code: 0,
                        cart: cartId
                    });
                }, 
                function(message){
                    sendErrorMessage(res, message);
                });

            }, 
            function(message){
                sendErrorMessage(res, message);
            });               
        }else{
            sendErrorMessage(res, "Params to create a cart and add an item to it is not suffisiant or not well formed.");
        }
    }
);

我在 addItemsToCart 的回调中收到错误。甚至都没有保存记录。但是,如果我删除了这部分代码:

return res.json({
                   code: 0,
                   cart: cartId
                });

没有错误,并且记录已正确保存。

0 个答案:

没有答案