Loopbackjs 在Loopback.js中,如果远程方法有一个返回参数,如何用空的主体响应它?

Loopbackjs 在Loopback.js中,如果远程方法有一个返回参数,如何用空的主体响应它?,loopbackjs,strongloop,Loopbackjs,Strongloop,如果我有这样的远程方法: Command.remoteMethod('invoke', { http: {verb: 'post', status: 200, source: 'body'}, returns: {arg: "text", type: "string"} }); Command.invoke = callback => { // ... if (error) { callback(null, 'There was an error'); }

如果我有这样的远程方法:

Command.remoteMethod('invoke', {
  http: {verb: 'post', status: 200, source: 'body'},
  returns: {arg: "text", type: "string"}
});
Command.invoke = callback => {
  // ...

  if (error) {
    callback(null, 'There was an error');
  } else {
    callback(null);
  }
}
有时我们需要用
text
参数来响应,有时则需要一个完全空的主体。在远程方法代码中,我有如下内容:

Command.remoteMethod('invoke', {
  http: {verb: 'post', status: 200, source: 'body'},
  returns: {arg: "text", type: "string"}
});
Command.invoke = callback => {
  // ...

  if (error) {
    callback(null, 'There was an error');
  } else {
    callback(null);
  }
}
问题是,在
else
分支中,主体从不为空。我也尝试过:
callback(null,null)
callback(null,”)


有没有办法做到这一点?或者我需要实现一个远程钩子来手动修改响应以获得我想要的

在model.js中定义
返回
块时,意味着您的远程方法有一个响应主体

对于您的情况,您可以删除远程挂钩的结果

Command.afterRemote("invoke", function(ctx, instance, next){
        //check if you want return text or nothing
        //if nothing so set result to null, otherwise just call next()
        ctx.result = null;
        next();
    });

最好的方法是使用后远程功能

如果没有内容,则可以添加

ctx.res.statusCode = 204
ctx.res.end(null);

调用
ctx.res.end()
意味着之后调用
next()
将是不可操作的,对吗?这仍然提供了一个非空响应