Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda