代码之家  ›  专栏  ›  技术社区  ›  Karlom

在vue.js方法中未接收socket.io回调?

  •  0
  • Karlom  · 技术社区  · 7 年前

    使用此vue.js方法登录用户:

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

    在服务器上 loginUser 事件处理者:

    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;
    
          });
    });
    

    我看得出来 'token was found' 在控制台中打印,但不接收在浏览器控制台中打印的房间。我也没有收到错误。

    我想知道这是由于vue.js方法的工作原理吗?如果是的话,如果有办法的话?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Abhishek Gupta    7 年前

    你忘了说明 rooms 作为回调中的参数

    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);
            });
        }