Home  >  Q&A  >  body text

Why are the callback functions in my nested functions not working?

I'm using Socket.io's callback function, like this:

loadData(callback) {

var client = new SyncClient(this.socket, this.project);
this.client = client; //Data function from me

    client.on("connected", () => {
        this.values ​​= client.getData();
        callback(client);
    }
}

But when I call my function loadData, this error message appears in the console: Uncaught TypeError: callback is not a function.

I think callback() is trying to call it's parent which is a function created inside client.on instead of loadData( callback)? Or does the problem lie elsewhere?

Call my loadData() like this in my mounted:

mounted() {
    this.loadData(this.client)
}

P粉957661544P粉957661544213 days ago352

reply all(1)I'll reply

  • P粉645569197

    P粉6455691972024-03-20 16:17:44

    Try using the data (res) received from the .find callback function and pass it to your callback function:

    socket.on('getSettings', function (data, callback) {
    
        console.log('Settings broadcast.');
    
        lmsSettings.find({}, function (err, res) {
            callback(false, res);
        });
    });

    reply
    0
  • Cancelreply