Javascript 环回模拟会话

Javascript 环回模拟会话,javascript,express,session-variables,loopback,Javascript,Express,Session Variables,Loopback,我正在使用带express会话的环回来存储cartId 但我需要在请求会话时注入cartId,以便使测试正常工作 所以在我的远程方法中 Cart.get = function (req, cb) { Service.getCart(req, req.session.cartId) .then(function (result) { cb(null, result); }) .catch(cb); }; Cart.remoteMethod(

我正在使用带express会话的环回来存储cartId

但我需要在请求会话时注入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进行测试


谢谢

如果我正确理解了您的案例,您可以执行类似于下面代码的操作,您只需向get方法定义中添加另一个参数(carid):

 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' }
    });
您可以简单地使用“get”远程方法并通过URL传递cartId,或者如果您担心cartId在URL上的可见性,那么您可以使用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检索购物车。 希望这能奏效