2016-03-21 3 views
1

モデルに静的でないリモートメソッドを追加しようとしています。ここにcodeに従ってください。残念ながら、私はいくつかのエラーメッセージを受け取りました。loopback非静的リモートメソッドエラーを追加します。

次は私のコード

User.prototype.lastOrder = function(callback){ 
    console.log('print this instance object: ', this); 
    callback(null) 
}; 

User.remoteMethod('__get__lastOrder', { 
    isStatic: false, 
    accepts: [], 
    description: 'Get the latest order of the user', 
    http: { 
    path: '/lastOrder', 
    verb: 'get' 
} 

そして、私はhttp://localhost:3000/v1/users/1/lastOrderを呼び出すです。それは私に次のエラーを与える:remoteMethodへ

enter image description here

答えて

2

最初の引数は関数名です。あなたが定義したものは有効ではありません。あなたは、よく、のlastOrderをしましょうという関数を定義し、次にそのようなあなたのコードを変更する必要があります。

User.prototype.lastOrder = function() { 

} 

User.remoteMethod('lastOrder', { 
    isStatic:false, 
    //more stuff here 
} 
+0

こんにちは、私は、このようにまだ同じエラーを試してみました。 –

+0

下記の私のコメントを参照してください。あなたは新しい回答を投稿しました。それがあなたのために働いたのですか? –

2
User.prototype.lastOrder = function(callback){ 
    console.log('print this instance object: ', this); 
    callback(null, "this is a test"); 
    }; 

    User.remoteMethod('lastOrder', { // should be lastOrder not __get__lastOrder 
    isStatic: false, 
    accepts: [], 
    description: 'Get the latest order of the user', 
    http: { 
     path: '/lastOrder', 
     verb: 'get', 
     status: 200 
    }, 
    returns: {root: true, type: 'order'} 
    }); 
+0

これはあなたのために働いたと言いますか? –

+0

@ RaymondCamden。はい。これは静的メソッドなので、 'User.prototype.lastOrder'にする必要があります。私はあなたがタイプミスをしたと思ったので、すべての正解を投稿します。誤解を招くようなことを避けたいだけです。とにかく、私はあなたの答えを受け入れますが、私はあなたの答えを修正します。 –

+0

ご清聴ありがとうございます。私は、たとえ非静的であっても、すべてのケースでModel.NAMEを使用すると書かれていたと誓っていたかもしれませんが、プロトタイプの下にあることは意味があります。 –

関連する問題