2017-03-05 8 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' 
     } 
:あなたはこのようなリモートメソッドを変更する必要が

関連する問題