【问题标题】:loopbackjs - findById requires the id argumentloopbackjs - findById 需要 id 参数
【发布时间】:2017-03-05 11:30:02
【问题描述】:

我是环回的新手,我正在尝试在环回中创建一个使用 findyById 方法的简单远程方法。在这上面花了几个小时,仍然无法让它工作。这是我的代码:

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.list(1)
            .$promise
            .then(function(response){
                console.log(response);
            })
            .catch(function(err){
                console.log(err);
            });

mysql 中我的客户行:

id:1 个数字: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)

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

【问题讨论】:

    标签: mysql node.js loopbackjs


    【解决方案1】:

    get 动词没有主体。

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

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

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 2018-06-22
      • 1970-01-01
      相关资源
      最近更新 更多