我有一个服务,其中包含对服务器发出请求的方法:
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)
你需要在add
函数中传递两个参数 - 首先是数据,其他是回调函数。你只传递一个。你需要传递两个这样的参数,
genresService.add( data, function (data) {
// TODO
});
答案 1 :(得分:2)
'添加'函数需要2个参数:data&回调:
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
});
答案 2 :(得分:2)
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);
});
答案 3 :(得分: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);
}
试试吧