2017-02-06 11 views
-1

私は、サーバーへのリクエストをした方法でサービスを持っている:

this.add = function (data, cb) { 
      $http({ 
       method: 'POST', 
       url: path 
      }).then(function successCallback(response) { 
       cb(response); 

      }, function errorCallback(response) { 
       // TODO 
      }); 
     }; 

私が呼ぶときadd()として:私が手

genresService.add(function (data) { 
    // TODO 
}); 

エラー:行に

TypeError: cb is not a function 
    at successCallback (custom.js:329) 

cb(response); 
+0

コールバックを唯一の引数として渡していますが、関数は2つ必要です。 –

+2

ではなく、 'this.add'関数から' promise'を返すべきです。そのようにして、関数呼び出しの上で '.then'を使用して連鎖約束であなたの関数呼び出しを拡張可能にすることができます。 –

+1

上記のように、代わりに約束が使われるべきです。約束ベースのコードでのコールバックの使用は反パターンです。 – estus

答えて

1
this.add = function (data, callback,error) { 
    $http({ 
     method: 'POST', 
     url: path, 
     data: data 
    }).then(callback).catch(error); 
}; 
//then call like this 
genresService.add(myData ,function (res) { 
     console.log(res); 
     } 
    ,function(errorResponse){ 
     console.log(errorResponse); 
}); 
2

add関数で2つのパラメータを渡す必要があります。最初はdataであり、otherはコールバック関数です。あなたは1つしか通過していません。あなたは、このような二つの引数を渡す必要があり、

genresService.add(data, function (data) { 
    // TODO 
}); 
2

'追加' 機能は、2つのパラメータを想定しています。データ&コールバック:

genresService.add(data,function (response) { 
    // TODO use response.data I presume 
}); 

はたぶん、あなたは何をしたい:

this.add = function (dataToPost, cb) { 
      $http.post(path,dataToPost) 
      .then(function successCallback(response) { 
       cb(response.data); 

      }, function errorCallback(response) { 
       // TODO 
      }); 
     }; 

genresService.add(someData,function (data) { 
    // TODO use data I presume 
}); 
0
this.add = function (jsonobj, callback) { 
     $http({ 
      method: 'POST', 
      url: path, 
      data: jsonobj 
     }).then(function(res) { 
      callback(res); 

     }, function(err) { 
      callback(err) 
     }); 
    }; 


//missing data like up : i call it jsonobj and finction got res is a callback 
genresService.add(jsonobj ,function (res) { 
    console.log(res); 
} 

試してみる

関連する問題