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

112
Visualizações
How to pass response back up MVC when receiving undefined

I'm trying to pass something down from app to controller and then model and then pass it back up, I'm moving down the chain fine into models and getting what I want to send back up, but then my controller is returning undefined. I'm aware I'm not returning the promise properly but can't quite see why I'm not returning.

Controller.js

const { fetchOwnerById } = require('./models')

const handleGreeting = (request, response) => {
    response.status(200).send({ message : 'hello from my api'})
}

const getOwnerById = (request, response) => {
    const ownerId = request.params['id'];
        fetchOwnerById(ownerId)
        .then((owner) => {
            console.log(owner)
            response.status(200).send({owner : owner})
        })
    }


module.exports = { handleGreeting, getOwnerById }

models.js

const fs = require('fs/promises')

const fetchOwnerById = (ownerId) => {
    console.log(ownerId)
    fs.readFile(__dirname + `/data/owners/${ownerId}.json`, 'utf-8').then((data) => {
        console.log(data)
        return data
        // response.status(200).send(data)
        })
}

module.exports = { fetchOwnerById }

I'm receiving TypeError: Cannot read properties of undefined (reading 'then')

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The fetchOwnerById method doesn't return anything, so it returns undefined, you need to return a promise of data so you can call then.

const fs = require('fs/promises')

const fetchOwnerById = async (ownerId) => {
    console.log(ownerId)
    return await fs.readFile(__dirname + `/data/owners/${ownerId}.json`, 'utf-8')
}

module.exports = { fetchOwnerById }

almost 4 years ago · Santiago Trujillo 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