Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
Getting throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);

I am using socket.io and getting this error:

throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received undefined

I am making a controller for the callbacks on the socket, this is how it looks:

const { onDisconnect } = require('./app/controllers/socket');

/*
...
*/

io.on('connection', (socket) => {
    socket.on('disconnect', onDisconnect(socket));
});

I get the error right on the "onDisconect" function. However if I declare the function like this, it works:

io.on('connection', (socket) => {
    socket.on('disconnect', () => {
        console.log(`${socket.id} disconnected`);
    });
});

I have the "onDisconect" function on another script, which contains this:

const onDisconnect = (socket) => {
    console.log(`${socket.id} disconnected`);
}

module.exports = { onDisconnect };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That's because you're calling onDisconnect(), which returns undefined, which is not a function. You have two options:

  1. Simply change socket.on("disconnect", onDisconnect(socket)) to socket.on("disconnect", onDisconnect)
  2. Return a function in onDisconnect:
function onDisconnect(socket) {
  return () => {
    console.log(`${socket.id} disconnected`);
  };
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda