使用loopback获取url中的参数的远程方法。

时间:2021-09-24 19:45:35

i'm trying to add a get remote method to my api using loopback 2.0 in order to achieve the same method structure as the default ones, such as :

我正在尝试使用loopback 2.0向我的api添加一个get remote方法,以实现与默认方法相同的方法结构,例如:

/myObject/{id}

The way I've tried is :

我试过的方法是:

  MyObject.remoteMethod(
    'remotemethod', {
      http: {
        path: '/',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'string'},
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )

But it only allows me to do this :

但它只允许我这样做:

http://localhost:3000/api/myObject?id=1

Does anyone knows how i can achieve this ?

有人知道我是如何做到这一点的吗?

Does someone also know how i can add a description to this route to display in the explorer ? The documentation doesn't really say much about this.. I think their documentation is not complete, am i the only one who feels that way ?

有人知道我如何在资源管理器中显示此路径的描述吗?文档并没有说明太多。我认为他们的文件不完整,我是唯一有这种感觉的人吗?

2 个解决方案

#1


2  

Answer for loopback 3.0 (but I assume it works similar for 2.0)

回答loopback 3.0(但我假设它适用于2.0)

MyObject.remoteMethod(
    'remotemethod', {
      description: 'This will insert the description',
      http: {
        path: '/:id',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'number', required: true},
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )

The trick is to add a required attribute to your id parameter and include the param in the path.

诀窍是在您的id参数中添加一个必需的属性,并将param包含在路径中。

Also see example on how to add description

还请参见如何添加描述的示例。

I do have to agree that the docs are quite incomplete still..

我不得不承认,这些文件还不完整。

#2


0  

You can annotate every single parameter that you want separately.

您可以分别注释您想要的每个参数。

for instance

例如

MyObject.remoteMethod(
    'remotemethod', {
      http: {
        path: '/',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'string', http: {source: query}},
        {arg: 'arg2', type: 'anything', http: {source: query}}
        ......
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )

#1


2  

Answer for loopback 3.0 (but I assume it works similar for 2.0)

回答loopback 3.0(但我假设它适用于2.0)

MyObject.remoteMethod(
    'remotemethod', {
      description: 'This will insert the description',
      http: {
        path: '/:id',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'number', required: true},
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )

The trick is to add a required attribute to your id parameter and include the param in the path.

诀窍是在您的id参数中添加一个必需的属性,并将param包含在路径中。

Also see example on how to add description

还请参见如何添加描述的示例。

I do have to agree that the docs are quite incomplete still..

我不得不承认,这些文件还不完整。

#2


0  

You can annotate every single parameter that you want separately.

您可以分别注释您想要的每个参数。

for instance

例如

MyObject.remoteMethod(
    'remotemethod', {
      http: {
        path: '/',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'string', http: {source: query}},
        {arg: 'arg2', type: 'anything', http: {source: query}}
        ......
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )