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?
I give credit to @codemonkey for giving me this suggestion which worked. I replaced res.send(data) with res.json(data).