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

110
Visualizações
Variable undefined as result of API call in JS

I am trying to get the dataValues part of an JSON object that is returned by this function that is called by an API call:

exports.findOne = (req, res) => {
    const id = req.params.id;

    Users.findByPk(id)
        .then(data => {
            if(data){
                console.log("data: ", data);
                res.send(data);
            } else {
                res.status(404).send({
                    message: "Cannot find user with id=${id}"
                });
            }
        })
        .catch(err => {
            res.status(500).send({
                message: err.message || "Error retrieving Tutorial with id=" + id
            });
        });
};

This is the output for when I print out data:

findOne:  Login {
  dataValues: {
    ID: 20,
    username: 'phil_swift',
    email: 'philswift@flextape.com',
    password: '<ENCRYPTED>',
    account_type: 1,
    token: null,
    token_expiration: null,
    require_password_at_login: false
  },
  _previousDataValues: {
    ID: 20,
    username: 'phil_swift',
    email: 'philswift@flextape.com',
    password: '<ENCRYPTED>',
    account_type: 1,
    token: null,
    token_expiration: null,
    require_password_at_login: false
  },
  uniqno: 1,
  _changed: Set(0) {},
  _options: {
    isNewRecord: false,
    _schema: null,
    _schemaDelimiter: '',
    raw: true,
    attributes: [
      'ID',
      'username',
      'email',
      'password',
      'account_type',
      'token',
      'token_expiration',
      'require_password_at_login'
    ]
  },
  isNewRecord: false
}

On the front end, when I perform this GET request, it returns undefined.

const { dataValues } = await httpCommon.get("/" + this.state.user.userID);
console.log("dataValues: ", dataValues);

How come this happens and how can I fix this?

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

0

I give credit to @codemonkey for giving me this suggestion which worked. I replaced res.send(data) with res.json(data).

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