Just a bit of confusion on my part. Sequelize seems to return an array that looks something like this:
[
Job {
dataValues: {
id: 2,
title: 'some job'
},
// ... more model variables
},
Job {
dataValues: {
id: 3,
title: 'another job'
},
// ... more model variables
}
]
But I can access each jobs properties by ignoring the dataValues
object.
So results[0].title
= ‘some job’
and results[0].dataValues.title
= ‘some job’
But results[0].title
doesn’t actually exist - or at least I can’t see it when I output results[0]
This doesn’t really matter that much, but when I tried to format the createdAt
col I found I could only access it using results[0].dataValues.date
, (results[0].date
returned undefined)
Something like this:
exports.test = (req, res, next) => {
Job.findAll({
attributes: [
'title',
'createdAt',
[Sequelize.fn('date_format', Sequelize.col('job.createdAt' ), '%d/%m/%y'), 'date'] // *Only* accessible with inside the dataValues object
]
}).then(results => {
console.log(results[0].date); // undefined
console.log(results[0].dataValues.date); // '09/06/21'
res.status(200).send();
}).catch(err => next(err));
}
Again, it’s not that important, but I feel like I should really understand something this basic if I’m going to be using an ORM like this
Anyone got any ideas?
Ta,
Nick