首页 > 解决方案 > 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
                });

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

标签: node.jsexpress

解决方案


推荐阅读