首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >loopbackjs - findById需要id参数

loopbackjs - findById需要id参数
EN

Stack Overflow用户
提问于 2017-03-05 11:08:36
回答 1查看 1.3K关注 0票数 0

我是loopback的新手,我正在尝试使用findyById方法在loopback中创建一个简单的远程方法。我花了几个小时在这上面,但还是不能让它工作。下面是我的代码:

customer.js:

代码语言:javascript
复制
    Customer.list = function(customerId, cb){
       app.models.Customer.findById(customerId, function (err, instance) {
          cb(null, err || 'success');
          if(err){
             console.log(err);
          }else if(instance){
             console.log(instance);
          }
       });
    }

    // expose the above method through the REST
   Customer.remoteMethod('list', {
       returns: {
          arg: 'list',
          type: 'string'
       },
       accepts: {arg: 'id', type: 'number', http: { source: 'query' } },
       http: {
          path: '/list',
          verb: 'get'
       }
   });

customer.controller.js:

代码语言:javascript
复制
    Customer.list(1)
            .$promise
            .then(function(response){
                console.log(response);
            })
            .catch(function(err){
                console.log(err);
            });

mysql中的My customer行:

id: 1号码: 10

我得到了这个错误:

代码语言:javascript
复制
    Error: Model::findById requires the id argument
    at Desktop\SampleProject\node_modules\loopback-datasource-juggler\lib\dao.js:1287:10
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

您能告诉我出现此错误的可能原因吗?请帮帮我。谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-03-05 11:30:02

有了get动词,就没有身体。

您需要更改远程方法,如下所示:

代码语言:javascript
复制
 accepts: {arg: 'id', type: 'number', http: { source: 'path' } },
       http: {
          path: '/list/:id',
          verb: 'get'
       }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42604039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档