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

97
Visualizações
Scope of express response

I know this is a scope issue, but am not quite sure what I'm missing here.

I have a simple endpoint where, if a user is found I return a simple 'no user found message. Here is the function:

export const getOneUser = (req, res) => {
    const { id } = req.params;
    const foundUser = users.find((user) => user.id == id)
    checkUserExists(foundUser)
    res.send(foundUser)
}

I've replace my standard guard with a helper function. so instead of:

if(!userfound) res.send('No user was found')

with the following function:

const checkUserExists = (x) => {
    if(!x) res.send('No user found')
}

Since I am constantly using this guard I thought this might be a helpful function to write.

The problem is I'm getting the following error, no matter what I do, event if I import { respons } from 'express' and use that:

ReferenceError: res is not defined at checkUserExists (file:///Users/cnak/Desktop/PROJECTS/TEST3/controllers/users.js:7:12) at getOneUser (file:///Users/cnak/Desktop/PROJECTS/TEST3/controllers/users.js:18:5) at Layer.handle [as handle_request] (/Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/layer.js:95:5) at next (/Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/layer.js:95:5) at /Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/index.js:281:22 at param (/Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/index.js:360:14) at param (/Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/index.js:371:14) at Function.process_params (/Users/cnak/Desktop/PROJECTS/TEST3/node_modules/express/lib/router/index.js:416:3)

How do I create a helper function that can actually pass a response?

I've also tried to return the 'res.send('No user found')

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Because you are not defining res in your checkUserExists function.

const checkUserExists = (x, res) => {
    if(!x) res.send('No user found')
}

and

...
checkUserExists(foundUser, res);
...
about 4 years ago · Juan Pablo Isaza Relatório

0

If you are still getting error after sending res in checkUserExists method. Then problem is not here. You need to check your route, that you are passing actual http res instance here or not. Can you please paste your code of index.js and router file.

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