Karlom
Karlom

Reputation: 14864

socket.io callback is not recevied in vue.js method?

Using this vue.js method to login users:

   loginUser: function () {
        socket.emit('loginUser', {
            email: this.email ,
            password: this.password         
        }, function() {
            console.log('rooms in callback are:', rooms);
        });
    }

On the server the loginUser event is handled by:

socket.on('loginUser', (newuser,callback)=> {
  var body = _.pick(newuser, ['email', 'password']);
  console.log('body is:', body);
      User.findByCredentials(body.email, body.password).then((user) => {
        return user.generateAuthToken().then((token) => {

           if (token) {
                    console.log('token was found');
                     let rooms = ['Cats', 'Dogs', 'Birds'];
                    callback(rooms);

            } else {
               socket.emit('loginFailure', {'msg' : 'Login failure'});
            }
        }).catch((e) => {
          throw e;
        });
      }).catch((e) => {
           socket.emit('loginFailure', {'msg' : 'Login failure'});
           throw e;

      });
});

I can see 'token was found' printed out in the console but does not recieve the rooms being printed in the browser console. I receive no errors either.

I'm wondering whetehr it is due to how vue.js methods work? And if so, if there is a way around it?

Upvotes: 0

Views: 304

Answers (1)

Abhishek Gupta
Abhishek Gupta

Reputation: 1337

You forgot to specify rooms as argument in the callback

loginUser: function () {
        socket.emit('loginUser', {
            email: this.email ,
            password: this.password         
        }, function(rooms) { // need to have rooms argument
            console.log('rooms in callback are:', rooms);
        });
    }

Upvotes: 1

Related Questions