Loopbackjs 从以前的遥控器返回

Loopbackjs 从以前的遥控器返回,loopbackjs,strongloop,Loopbackjs,Strongloop,关于“beforeRemote”的环回方法,我有两个问题- 如何在beforeRemote方法中获得模型方法?我的意思是在beforemote中,我想调用(比如说)“upsert”模型的方法 如何从beforeRemote返回调用?我所说的返回是指执行将从beforeRemote方法返回,而不是命中目标调用的方法 我的代码- Installation.beforeRemote("create", function (context, result, next) { var data =

关于“beforeRemote”的环回方法,我有两个问题-

  • 如何在beforeRemote方法中获得模型方法?我的意思是在beforemote中,我想调用(比如说)“upsert”模型的方法
  • 如何从beforeRemote返回调用?我所说的返回是指执行将从beforeRemote方法返回,而不是命中目标调用的方法
  • 我的代码-

    Installation.beforeRemote("create", function (context, result, next) {
        var data = context.req.body;
        console.log("ImEI" + data.imei);
        data.vendor = "jahid";
    
        var self = this;
        var filter = {where: {imei: data.imei}};
    
        //the self here point to global object. but i want self to point to model
        self.findOne(filter, function (err, result) {
            if (result) {
                data.id = result.id;
                self.upsert(data, function(err, result){
                if(err){
                    next(err);
                } else if(result) {
            //here i want to send a valid response back to client with 200 and body as my model.
                    next(data);
                }
            });
            return;
        }
        next();
    });
    });
    
  • 您可以通过module.exports声明访问安装模型:

    module.exports = function(Installation) {
      ...
      Installation.upsert...
      ...
    }
    
  • 您可以从
    上下文
    对象访问响应对象。因此,您可以只使用类似于
    context.res.send('hello world')
    的响应,而不调用
    next()


  • 我会试试这个,然后带着反馈回来。非常感谢你的帮助。