I tired to call
Parse.Users.currentUser()
But i get error like
"Cannot call currentUser() when using a platform with an async storage system. Call currentUserAsync() instead."
Even i tried to look into Parse-React Repo. That is not active anymore and doesnt support 1.9 or above.
Even currentUserAsync() not working.. :(
WHAT do you mean not working? Can you provide more details please?
This is the Small code i tried to run in my RN 0.48
var currentUser = Parse.User.currentUser();
if (currentUser) {
// do stuff with the user
console.log("currentUser")
} else {
// show the signup or login page
console.log("Not logged in")
}
I get this error:
[Unhandled Promise rejection: Error: Cannot call currentUser() when using a platform with an async storage system. Call currentUserAsync() instead.]
Which is the 1st part, what happens when you call the suggested method?
Ok Actually i am first calling facebook api to get details and wanted to login the user . Here is my code. I am using async function.
//Facebook Login
```
async fbLogIn() {
this.setState({isLoggingIn : true});
const { type, token,expires } = await Expo.Facebook.logInWithReadPermissionsAsync(FbAppId, {
behavior:"native"
});
if (type === 'success') {
// Get the user's name using Facebook's Graph API
const response = await fetch(
`https://graph.facebook.com/v2.10/me?fields=id,name,friends,email&access_token=${token}`);
let expiresUTCSeconds = expires;
let expireDate = new Date(0); // The 0 there is the key, which sets the date to the epoch
expireDate.setUTCSeconds(expiresUTCSeconds);
//Forming UserDetails
console.log('response')
console.log(response)
let fbResponse = await response.json();
console.log(fbResponse)
let userDetails = {fbToken: token, fbExpire: expireDate,name: fbResponse.name, id: fbResponse.id ,email:fbResponse.email};
//Storing local
await AsyncStorage.setItem(USER_DETAILS, JSON.stringify(userDetails));
await AsyncStorage.setItem(USER_STATE, "true")
// // these are the data from the successful FB login we will pass to Parse.FacebookUtils.logIn instead of null
// // based on https://github.com/ParsePlatform/ParseReact/issues/45#issuecomment-111063927
let authData = {
id: userDetails.id,
access_token: userDetails.fbToken,
expiration_date: userDetails.fbExpire
};
var userid = userDetails.id;
var email = userDetails.email;
var profilename = userDetails.name;
Parse.User.current().set("userid", userid);
Parse.User.current().set("email", email);
Parse.User.current().set("profilename", profilename);
Parse.User.current().save();
this.setState({isLoggingIn : false});
this.props.navigation.navigate("SignedIn")
} else {
Alert.alert(
'Facebook Login Failed!',
`Error 102`,
);
this.setState({isLoggingIn : false});
}
}
```
The Problem is i am getting the following error when i use Parse.User.current() inside async function.
[Unhandled Promise rejection: Error: Cannot call currentUser() when using a platform with an async storage system. Call currentUserAsync() instead.]
Why don鈥檛 you call currentUserAsync() as it鈥檚 what鈥檚 exposed by the SDK?
Yes i did tried out..
Parse.User. currentUserAsync().set("userid", userid);
Parse.User. currentUserAsync().set("email", email);
Parse.User. currentUserAsync().set("profilename", profilename);
Parse.User. currentUserAsync().save();
it throws it not a funciton.
but when i use
Parse.User. currentAsync();
It returns the User details.
but still it throwns set is not a function.
Parse.User.currentAsync().set("userid", userid);
Seriously, i dont know what i am missing here.
I believe currentUserAsync() returns a Promise. So this should be:
Parse.User.currentAsync().then(function(user) {
user.set(...);
...
return user.save();
});
wow.. that worked :)
Parse.User.currentAsync() still isn't in the docs for some reason
@MiLeung
Yeah. Even I was confused bcoz of no documentation. :) Hope they will add soon.
@MiLeung @jinuem noted the docs issue and opened up a PR to resolve that.
Alright, should be good now. This is now documented in the current user section of the JS docs. Thanks for bringing this to our attention!
You can't call currentAsync outside async function, for example in your facebook login code you used async fbLogIn() {...} so since this is async function, you have to use currentAsync
Most helpful comment
I believe currentUserAsync() returns a Promise. So this should be: