loopbackjs-findById需要id参数 [英] loopbackjs - findById requires the id argument

查看:256
本文介绍了loopbackjs-findById需要id参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是回送的新手,我试图在回送中创建一个使用findyById方法的简单远程方法.在此上花费了几个小时,但仍然无法正常工作.这是我的代码:

I am new to loopback and I am trying to create a simple remote method in loopback that uses findyById method. Been spending couple of hours on this and still can't get it to work. Here is my code:

customer.js:

customer.js:

    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:

customer.controller.js:

    Customer.list(1)
            .$promise
            .then(function(response){
                console.log(response);
            })
            .catch(function(err){
                console.log(err);
            });

我在mysql中的客户行:

My customer row in mysql:

id:1个数字:10

id: 1 number: 10

我收到此错误:

    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)

能否请您告诉我我收到此错误的可能原因? 请帮我.谢谢

Can you please tell me the possible reason/s why I get this error? Please help me. Thank you

推荐答案

使用get动词时,没有身体.

With get verb, there is no body.

您需要像这样更改远程方法:

You need to change the remote method like this :

 accepts: {arg: 'id', type: 'number', http: { source: 'path' } },
       http: {
          path: '/list/:id',
          verb: 'get'
       }

这篇关于loopbackjs-findById需要id参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆