2017-04-13 48 views
0

我正在使用快速会话环回来存储cartId。Loopback模拟会话

但我需要注入cartId请求会话,以使我的测试工作。

所以在我的远程方法我有

Cart.get = function (req, cb) { 
    Service.getCart(req, req.session.cartId) 
    .then(function (result) { 
     cb(null, result); 
    }) 
    .catch(cb); 
}; 

Cart.remoteMethod(
    'get', 
    { 
     accepts: { arg: 'req', type: 'object', 'http': { source: 'req' } }, 
     returns: { arg: 'cart', type: 'object', root: true }, 
     http: { path: '/', verb: 'get' } 
    } 
); 

如何强制req.session.cartId我的测试?

感谢

回答

0

如果我正确理解你的情况,你可以做类似下面的代码,你只需添加一个参数(CardId中)到你的get方法定义的东西:

Cart.remoteMethod('get',{ 
     accepts: [ 
     { arg: "caseId", type: "number", http: {source:'path'} }, 
     { arg: 'req', type: 'object', http: { source: 'req' } } 
     ], 
     returns: { arg: 'cart', type: 'object', root: true }, 
     http: { path: '/:caseId/getCart', verb: 'get' } 
    }); 
0

你可以只需使用“get”远程方法并通过URL传递cartId,或者如果您对URL的cartId可见性有所顾虑,则可以使用post方法作为以下代码。 使用以下cart.js文件并探索回送API。

module.exports = function (Cart) { 

    Cart.getCart = function (cartId, cb) { 
    Cart.findOne({ 
     where: { cartId : cartId } 
    }, function (err, cart) { 
     cb(null, users); 
    }); 
    }; 

Cart.remoteMethod('getCart', { 
    accepts: { 
     arg: "id", 
     type: "string", 
     required: true 
    }, 
    returns: { 
     arg: 'cart', 
     type: 'object' 
    }, 
    http: { 
     path: '/:cartId/getcart', 
     verb: 'get' 
    } 
    }); 

}; 

get call : http://HOST:IP/cart/YourID/getcart 

您将通过Id检索购物车。 希望这会起作用。