Call to Socket.IO from register.js returning err, as an empty object { }

Plugin Development
  • I'm making a call from register.js to socket.io using the following function

    function validateInviteCode(code, use, callback) {
    	callback = callback || function() {};
    	var invite_code_notify = $('#invite_code_notify');
    	
    	if (!code.includes(':') || code.split(':')[1].length != 8) {
    		showError(invite_code_notify, 'Invalid invite code');
    		return callback();
    	}
    	
    	socket.emit('plugins.invite.checkInviteCode', {
    		code: code,
    		use: use
    	}, function(err, valid) {
    		if (err) {
    			console.log(err);
    			app.alertError(err.message);
    			return callback();
    		}
    		
    		console.log(valid);
    		if (valid) {
    			showSuccess(invite_code_notify, successIcon);
    		} else {
    			showError(invite_code_notify, 'Invalid invite code');
    		}
    		
    		callback();
    	});
    }
    

    and it's returning err as an empty object { }. This is my server-side listen for it:

    checkInviteCode: function (socket, data, fn) {
    checkCode(data.code, data.use, function(valid) {
    console.log(data, valid);
    fn(!socket.uid, valid);
    });
    }

  • In your server code, looks like your responding with a boolean value !socket.uid when it expects an Error object or null. You can use a ternary operator ? : to check the uid and send the appropriate response.

    Also, the data parameter should always be a plain object, and not a literal value.

    e.g.
    fn(!socket.uid ? new Error("Invalid uid") : null, {valid: valid})

  • Ooops! I'm sorry haha, this headache is giving me coding troubles. Thanks!


Suggested Topics


  • 0 Votes
    7 Posts
    701 Views
  • 0 Votes
    7 Posts
    498 Views
  • 0 Votes
    3 Posts
    1219 Views
  • 0 Votes
    2 Posts
    1023 Views
  • 4 Votes
    20 Posts
    7740 Views