Parse User.Login never gets called? - javascript

I'm simply trying to Log in with a user I've just created in Parse.
I know the user is valid, I know the function gets called.
This is what I tried:
// V1
Parse.User.logIn("username", "password").then(function(user) {
console.log("Logged in");
}, function(error) {
console.log("Error logging in");
});
// V2
Parse.User.logIn("username", "password", {
success: function(user) {
console.log("Logged in");
},
error: function(user, error) {
console.log("Error logging in");
}
});
And neither of the console logs show up, success or failure...
Is there something I'm missing?
Thanks!

This was in fact due to the asynchronous nature of the Parse.User.Login code, had to work with it as a promise
For others having this issue: http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/

Related

Problem handling Parse users in a web app

Here is some JS code, related to handling Parse users in a web app, with unexpected behaviour.
......
Parse.initialize(process.env.APP_ID);
Parse.serverURL = process.env.SERVER_URL;
Parse.User.logIn(req.body.usrname, req.body.password, {
success: user => {
console.log("OK-OK:"+user);
checkIfLoggedIn();
res.render('pages/index.ejs', {});
},
error: (user, error) => {
console.log("NG-NG:"+user, error);
checkIfLoggedIn();
res.render('pages/login.ejs', {});
},
});
......
function checkIfLoggedIn() {
var currentUser = Parse.User.current();
if (currentUser) {
console.log("Parse.User.current is A REAL USER -- logged in!!")
} else {
console.log("Parse.User.current is NULL -- login failed!!")
}
}
Now, below is the question.
When running the code above, the messages:
"OK-OK:" and "NG-NG:" both show up in the logs as expected. But the call to the function checkIfLoggedIn() always displays a failure:
Parse.User.current is NULL -- login failed!!
Why is that? Shouldn't it display a success when we reach "OK-OK:"?

How to handle error in Angular Controller from MongoDB database update/delete in Express?

I am trying to figure out how to handle an error when deleting or updating a document in MongoDB in Angular JS?
I have the following route in Node/Express:
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
//log the reason for the error
res.status(code || 500).json({
"error": message
});
}
app.delete("/polls/:id", auth, function(req, res) {
db.collection(POLLS_COLLECTION).deleteOne({
_id: new ObjectID(req.params.id), userID: req.user.id
//userID must match the req.user.id from Passport to make sure the poll belongs to the user
}, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to delete poll");
} else {
res.status(204).end();
}
});
});
The following in an Angular JS controller:
$scope.deleteThisPoll = function(){
Polls.deletePoll($routeParams.pollId)
.then(function(response){
alert("Poll deleted!");
var url = "/mypolls/" + $scope.userID;
$location.path(url);
}, function(response){
alert("Error deleting poll");
console.log(response);
})
};
deleteThisPoll in the controller calls a deletePoll service that sends a a request to the route:
this.deletePoll = function(pollId){
var url = "/polls/" + pollId;
return $http.delete(url);
};
What I want is to alert "Error deleting poll" from the Angular controller when the database delete is not executed (because for example user is not authenticated or the poll doesnt belong to the user) and "Poll Deleted" when the delete was successfull.
However: the error callback is never used and the app always alerts "Poll deleted!" no matter if the document was deleted or not deleted.
Doesn't my route send an error response when the delete was not executed and will it not hit my Angular error callback?
You can do like code below
Put this HTML code where you want to show error message :
<div style="color:red;">
{{error}}
</div>
In your angular js controller :
$scope.deleteThisPoll = function(){
Polls.deletePoll($routeParams.pollId)
.then(function(response){
alert("Poll deleted!");
var url = "/mypolls/" + $scope.userID;
$location.path(url);
}, function(response){
$scope.error="Any error message you like to show";
console.log(response);
})
};
If your API return an error. you can catch it like this :
Polls.deletePoll($routeParams.pollId).then(function(response) {
//SUCCESS CODE
}).catch(function(error) {
//NOTIFY ERROR
//NotifyService.display(error);
console.log(error);
});
thanks guys. I found out that MongoDB for some reason always returns a result object even when there was no delete/update. I solved this by checking for the result.deletedCount propety that is set to 1 or 0. Like so:
if(err){
res.status(500).end();
}
if(result.deletedCount === 0){
res.status(404).end();
//error handling in Angular error callback
} else {
res.status(204).end();
//error handling in Angular success callback
}
});
});
this makes sure that not always a 204 is send whether or not the delete was successfull.

Cloud Code Not Updating User's Username Properly

For some reason Cloud Code isn't updating the current user's username, even though it is updating the email field. I'm using the master key, and although everything returns success, the username doesn't update (even on the data browser).
Here's my code:
//Get Current User
var user = Parse.User.current();
//Update username
user.set("email", request.params.email);
user.set("username", request.params.username);
user.save(null, {
//Success
success: function(user) {
//Done
response.success("Username saved! 🎉");
},
//Error
error: function(user, error) {
// Execute any logic that should take place if the save fails.
response.error("Aww man. Something went wrong. Please try again. 😅");
}
});
I've made sure that the parameters are being passed correctly, and that there isn't a mistake with the name etc on my iOS app.
My guess is that there is an issue with getting the calling user.
Use request.user to get the calling user and try the following.
// Get the requesting user
var user = request.user;
if (user) {
user.set("email", request.params.email);
user.set("username", request.params.username);
user.save(null, {
success: function(user) {
response.success("Username saved! 🎉");
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
} else {
response.error("Aww man. Something went wrong. Please try again. 😅");
}

Parse.com cloud bad request 400

I use Parse.com cloud to manage my database for mobile application. When I save in table user some user with the same username or email it gives me the error:
POST https://api.parse.com/1/users 400 (Bad Request)
I understood by myself that error appears when the username or email are the same in different users. Is there a method to return the reason of the error like "this mail is already chosen"? Below my code:
saveUser: function() {
this.utente.save(null, {
success: function(persona) {
//console.log("modello salvato nel db");
var id = persona.get("objectId");
window.localStorage.setItem('parseId', id);
},
error: function(error) {
alert("Save error");
console.log(error);
}
});
},
Looks like you aren't using response.error(error) anywhere...
Try
saveUser: function() {
this.utente.save(null, {
success: function(persona) {
//console.log("modello salvato nel db");
var id = persona.get("objectId");
window.localStorage.setItem('parseId', id);
},
error: function(error) {
response.error(error);
}
});
}
And then in your native script console.log the error.code and error.message.

$scope changes in .success() not applying

Parse.User.logIn($scope.user.username, $scope.user.password, {
success: function (user) {
console.log("logged in");
$scope.User = user;
$scope.$apply();
},
error: function (user, error) {
console.log(error);
}
})
In the view, I have {{User.attributes.username}} that right after login shows nothing, but when someones already logged in and views the page, it works fine.
The scope doesn't change when someone just logs in. But above, I already have $scope.User = Parse.User.current() and that works fine. It seems to be an issue with this particular instance of changing the scope.
Try this:
success: function (user) {
console.log("logged in");
$scope.$apply(function() {
$scope.User = user;
});
},

Categories

Resources