2017-04-13 45 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檢索購物車。 希望這會起作用。