Ionic Hybrid cometchat.getLoggedinUser() throws error

When I receive a cometchat notification or open my app (after removing it from the task manager), I initialize cometchat on start. After it gives success, I check whether the user is already logged in or not using cometchat.getLoggedinUser() , this at times results a success. At times, the result is null.
When the result is null, I call the login method again.

The error I get on login is - COMETCHAT_INITIAIZATION_NOT_DONE, But I already Initialize it before, and on it’s success only the login method get’s called.
I have even tried setting a delay of 1s on login method after the initialization is complete, yet the login method throws the error - - COMETCHAT_INITIAIZATION_NOT_DONE.

1 Like

Hello @neeraj,

Can you please share the code snippet of CometChat.init(), CometChat.login() method and CometChat.getLoggedInUser(). We have tested this on our end and everything seems to be working as expected. We are testing on the latest SDK v2.0.5. Also, can you please let us know which version of the SDK are you using?

Comet chat init code - This runs as soon as I click on the notification and the app is not loaded in the task manager, on the start of the app, the following code runs.

async cometChatInit()  {

   await CometChat.init(this.appID, new CometChat.AppSettingsBuilder()

      .subscribePresenceForAllUsers()

      .setRegion(this.appRegion)

      .build()).then(

        () => {

          console.log('Initialization completed successfully');

After this gets initialized, I am calling the getlogged in user method.

           CometChat.getLoggedinUser().then((res) => {

            console.log("I am a logged in user" + JSON.stringify(res));

Checking if the app’s local storage and the cometchat uid is same, if yes, user is logged in.

            if (res != null && res['uid']== this.chatID){

              this.isCometLoggedIn = true;

            }else{

              this.isCometLoggedIn = false;

                // Make the user log in.

                setTimeout(() => {

                  this.logMeIn();

                }, 700);

            }

          }).catch((err) => {

            console.log(JSON.stringify(err));

            this.isCometLoggedIn = false;

          });

        }

      ).catch((err)=>{

        console.log(err);

      });

  }

My login method -

async logMeIn() {

   await   CometChat.login(this.chatID, this.apiKey).then(

      user => {

        console.log('Login Successful from api:'+ { user });

        this.isCometLoggedIn = true;

        setTimeout(() => {

Get’s the user’s chat list

           this.getUserList();

        }, 0);

        // this.storage.set("cometLogin", this.isCometLoggedIn);

        // this.loading.hide();

        // User loged in successfully.

      },

      error => {

        this.isCometLoggedIn = false;

My code alerts at this line, even though the app initializes and the getloggedinuser returns null.
At null, I make the login call.

        console.log("I got error from api"+ JSON.stringify(error));

        // this.loading.hide();

        // User login failed, check error and take appropriate action.

      }

    ).catch((err)=>{

      console.log("I got error from api"+ JSON.stringify(err));

    });

  }
1 Like

Hello @neeraj,

Can you also let us know which version of the cordova-ionic-sdk are you using?
It seems to be working with v2.0.5.

I am using - cometchat-pro/cordova-ionic-chat": “^2.0.4”
At times it works, but it isn’t stable. getLoggedInUser throws null, and then if I call login function, then it fails saying - Comet chat needs to be initialized before login method can be called.

1 Like

Hello @neeraj,

Please update to the latest version, it should work.

1 Like

Can you tell me the latest version?

1 Like

Hello @neeraj,

The latest version of ionic cordova SDK is v2.0.5.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.