Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!