0

So what I have is this: I want to keep all the 'parse' code inside a manager that I can call from another classes and files. On this example I have a function that will only check if the user is logged in and then return different drawer navigations based on that. The problem is that I keep getting the error 'undefined is not a function (evaluating ...)'. Im kinda new to javascript and couldn't find an answer to this. Here's the code.

Thanks in advance.

ParseManager.js

isUserLogged() {
Parse.User.currentAsync().then(function (user) {
    if (user) {
        console.log("ParseManager - User logged in.")
        return true;
    } else {
        console.log("ParseManager - User logged off.")
        return false;
    }
});

App.js

render() {
if (ParseManager.isUserLogged()) {
  return (
    <SecondaryRoot />
  );
} else {
  return (
    <MainRoot />
  );
}
4
  • on which line?? Commented Oct 30, 2018 at 11:56
  • post all the code, not only a part Commented Oct 30, 2018 at 11:57
  • 1
    isUserLogged is not doing what you think it's doing. It's returning undefined because Parse.User... is a promise. Commented Oct 30, 2018 at 12:01
  • @Wainage indeed it's returning undefined, how can i change this? Commented Oct 30, 2018 at 12:04

1 Answer 1

1

You simply call isUserLogged on component mount and later simply check the state: ParseManager.js

isUserLogged(callback) {
   Parse.User.currentAsync().then(function (user) {
     if (user) {
         console.log("ParseManager - User logged in.")
         callback(true);
     } else {
         console.log("ParseManager - User logged off.")
         callback(false);
     }
});

App.js

componentDidMount(){
    ParseManager.isUserLogged(
       (logged)=>{
         this.setState({logged});});
       });
}

render() {
  if (this.state.logged) {
    return (
      <SecondaryRoot />
    );
  } else {
    return (
      <MainRoot />
    );
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.