TypeError: Cannot read property 'userId' - sendbird

681 views Asked by At

i am trying to create a channel between 2 sendbird users using group channel. So far my implementation is

<script type="text/javascript">
    function chatInit(){
        var sb = new SendBird({
            appId: 'my app id'
         });

    sb.connect('test user','access token of user', function(user, error) {
    console.log(error);

    }); 
    var userIds = ['another user'];
    var name ="name of channel";
    sb.GroupChannel.createChannelWithUserIds(userIds, true, name ,'', '', function(channel, error) {
             if (error) {
        console.error(error);
    return;
}

});

}
</script>

and i get following error on console

SendBird.min.js:6 Uncaught TypeError: Cannot read property 'userId' of null
at Function.GroupChannel.createChannelWithUserIds 

am i missing something, please guide me through the process. any and all help will be appreciated.

1

There are 1 answers

0
eoamusan On

The reason why you get that error response is because the connect has not run to completion as at the time the code

sb.GroupChannel.createChannelWithUserIds()

is run.

You need to add that add that block of code inside the callback function of sb.connect() like so:

function chatInit(){
    var sb = new SendBird({
        appId: 'my app id'
     });

     var userIds = ['another user'];

     sb.connect('test user','access token of user', function(user, error) {
         console.log(error);

         if(user){
              var name ="name of channel";
              sb.GroupChannel.createChannelWithUserIds(userIds, true, name ,'', '', function(channel, error) {
                  if (error) {
                       console.error(error);
                       return;
                  }
              }); 
         }
     }
}