Problem with socket in plugin after 1.12.2 -> 1.13.0 upgrade


  • Hello !

    I'm trying to upgrade my nodebb 1.12.2 forum to 1.13.0 and I have after upgrade/build/start a bug with one of my plugins (which worked very well on 1.12.2).

    Error message is :

    2019-11-20T16:17:00.664Z [5678/15806] - error: uncaughtException: callback is not a function
    TypeError: callback is not a function
        at /home/yannick/MPstarClem/Developpement/nodebb/node_modules/nodebb-widget-online-users-YA/index.js:324:3
    

    When I look in my plugin code, there is a callback call at the line number 324. A console.log(callback) just before this line gives {} which is really strange (obviously, it's not a function...).

    Here is a simplified code to show how I get the problem... On the client side, there is a function which is called containing this :

    if (socket.connected) {
        socket.emit('plugins.onlineUsers.getOnlineUsers', function (err, data) {
            console.log('Just a try');
            }
        );
    }
    

    This function calls a getOnlineUsers function on server side which contains this :

    socketPlugins.onlineUsers.getOnlineUsers = function(socket, callback) {
            console.log(callback); // Problem since nodebb 1.13.0 : callback is already {} here !!
    	UpdateUsersOnline(socket.uid, callback); // This function doesn't work because callback is not a function... This gives the error...
    };
    

    I can't find what have changed in nodebb 1.13.0 since 1.12.2 that makes this problem (I saw nothing relevant in breaking changes).

    Any help ? Thanks !

  • Admin NodeBB

    Change your server function to below and it should work.

    socketPlugins.onlineUsers.getOnlineUsers = function(socket, data, callback) {

    I will see if I can fix this so it works with your current usage.


  • @baris Thanks ! Now it works... I think I have the same problem with others plugins I made. I will adapt them.

  • Admin NodeBB

    Keep in mind we support async/await now so you might want to just use this instead

    socketPlugins.onlineUsers.getOnlineUsers = async function(socket) {
        await UpdateUsersOnline(socket.uid);
    };
    

  • @baris Ok, I will have to change this on many plugins (I hope it will be not too difficult ; I never used async/await but if you talk about this to me, it may be greater than async module 😉 ).

    Edit : Mmmm, I tried your suggestion but I get again the error

    error: uncaughtException: callback is not a function
    TypeError: callback is not a function
    

    with it. I will have to look closer to functions arguments when going from async module to async/await...

  • Admin NodeBB

    Did you also update UpdateUsersOnline to not use a callback?

    If you are awaiting a function then it won't take a callback as the last argument, so inside UpdateUsersOnline you won't call callback anymore.


  • @baris OK. I changed this and it works now. Thanks!

Suggested Topics

| |