我正在尝试将数据存储在angularjs工厂的数据库中,这对我有用。在Parse中存储数据后,我试图返回1或错误对象(取决于存储是否工作),然后在控制器中处理它。
在我检查返回值的控制器中,它既不是1也不是错误,而是未定义的。
这是我的代码......
控制器呼叫服务:
$scope.signup = function(user, $state) {
if(user !== null)
{
var returnValue = CreateUserService.createAccount(user);
console.log("Return Value = " + returnValue);
if(returnValue === 1)
{
$state.go('activate');
}else{
console.log("Return Value was not appropriate");
}
}else{
console.log("Something went wrong while adding user");
}
};
厂:
.factory('CreateUserService', function() {
return {
createAccount: function(user) {
var parseUser = new Parse.User();
parseUser.set("username", user.username);
parseUser.set("email", user.email);
parseUser.set("name", user.name);
parseUser.set("password", user.password);
parseUser.set("mobile", user.mobile);
/*Attempt to create user in DB*/
parseUser.signUp(null, {
success: function(parseUser) {
// Hooray! Let them use the app now.
//alert("success!");
console.log("Stored");
return 1;
},
error: function(parseUser, error) {
// Show the error message somewhere and let the user try again.
//alert("Error: " + error.code + " " + error.message);
console.log("Error: " + error.message );
return error;
}
});
}
}
}
感谢您的帮助。
答案 0 :(得分:1)
您实际上并非从createAccount
函数返回,而是仅从回调中返回。回调也是asynchronous
,因此它会在完成请求之前返回。
.factory('CreateUserService', function($q) {
return {
createAccount: function(user) {
var deferred = $q.defer();
var parseUser = new Parse.User();
parseUser.set("username", user.username);
parseUser.set("email", user.email);
parseUser.set("name", user.name);
parseUser.set("password", user.password);
parseUser.set("mobile", user.mobile);
/*Attempt to create user in DB*/
parseUser.signUp(null, {
success: function(parseUser) {
// Hooray! Let them use the app now.
//alert("success!");
console.log("Stored");
deferred.resolve(1);
},
error: function(parseUser, error) {
// Show the error message somewhere and let the user try again.
//alert("Error: " + error.code + " " + error.message);
console.log("Error: " + error.message );
deferred.reject(error);
}
});
return deferred.promise;
}
}
}
然后在您的控制器中使用服务功能作为承诺。
CreateUserService.createAccount(user).then(function(result) {
// result should be whatever passed in to resolve
}, function(error) {
// error here will be whatever passed into reject
});
了解有关承诺的更多信息: https://docs.angularjs.org/api/ng/service/ $ Q
答案 1 :(得分:1)
我认为问题是因为你的ajax调用异步调用。当你调用" CreateUserService.createAccount(user)"时,它会调用ajax调用parseUser.signUp,这是异步的。
if(user !== null)
{
var promise = CreateUserService.createAccount(user);
promise.then(function(data) {
$state.go('activate');
},
function(data) {
console.log("Return Value was not appropriate");
});
}else{
console.log("Something went wrong while adding user");
}
};
使用此更改控制器 $ scope.signup = function(user,$ state){
$( ".fc-day-number" ).append("<a href='' class='addEventBtn' id='modalButton1'>+</a>");
这可能会解决问题
答案 2 :(得分:1)
问题是你从一个回调深度回归,所以你需要某种回调或angularJS&#39; $q service
类似问题:How to return value from an asynchronous callback function?
$scope.signup = function(user, $state) {
if(user !== null)
{
// notice the callback function passed below
CreateUserService.createAccount(user, function(returnValue){
// returnValue in available here for use
console.log("Return Value = " + returnValue);
if(returnValue === 1)
{
$state.go('activate');
}else{
console.log("Return Value was not appropriate");
}
});
}else{
console.log("Something went wrong while adding user");
}
};
厂:
.factory('CreateUserService', function() {
return {
createAccount: function(user, done) {
// done callback function in the param to be called for returning the values
var parseUser = new Parse.User();
parseUser.set("username", user.username);
parseUser.set("email", user.email);
parseUser.set("name", user.name);
parseUser.set("password", user.password);
parseUser.set("mobile", user.mobile);
/*Attempt to create user in DB*/
parseUser.signUp(null, {
success: function(parseUser) {
// Hooray! Let them use the app now.
//alert("success!");
console.log("Stored");
// call the done/callback here for success
done(1);
},
error: function(parseUser, error) {
// Show the error message somewhere and let the user try again.
//alert("Error: " + error.code + " " + error.message);
console.log("Error: " + error.message );
// call the done/callback here for error
done(error);
}
});
}
}
}