2017-03-05 111 views
0

我是新來的迴環,我試圖在使用findyById方法的迴環中創建一個簡單的遠程方法。花了幾個小時在這個上,仍然無法讓它工作。這裏是我的代碼:loopbackjs - findById需要ID參數

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) 

能否告訴我可能的原因/爲什麼我得到這個錯誤? 請幫幫我。謝謝

回答

1

get動詞,沒有身體。

你需要改變這樣的遠程方法:

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