How to access Sequelize results

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 :man_shrugging:

Anyone got any ideas?

Ta,

Nick

I haven’t used Sequelize myself. But as far as I can tell Job is a class. If the class has a get title() method , you’ll be able to access it like Job.title. I think if you log Job.toString() you’ll be able to see the class methods and get a better feel for the class. If dataValues is a property then you could also access dataValues.title as well. I’m going to assume that there is no get date() method and that’s why you have to access it via dataValues.

Can you post a link to the documentation for the library?

Ahhh, yeah that makes sense. I output the keys of the sequelize/model object using console.log(Object.keys(results[0].__proto__)); and in there was a variable/array called _customGetters - which makes me think you’re right, and might explain why my aliased date column isn’t accessible

sequelize docs

Tbh I’d really love to learn more about it in detail, but the docs feel rather… lacking.

In all honesty, coming from Java, JS in generally seems to have fairly ropey documentation. Maybe that’s unfair :smile:

If you’re coming from Java then you may find learning TypeScript is more natural. I’m not sure, I don’t know Java. But TS it typed, it’s just structural typing instead of nominal typing.

1 Like

Yeah, it’s on the list. But for some reason I decided to create a CRM-lite-type website for a friend in vanilla js

I really now see the value of React :sweat_smile:

Thanks for the help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.