How can I get the dataValues out of the response. I have tried doing console.log(result.dataValues) but it returns undefined.
Response
[ User {
  dataValues: {
    id: 16,
    user_id: '140235016357535420',
    server_id: '535881918483398676',
    xp: 40995,
    coins: 0,
    createdAt: 2019-03-09T22
    :
    59: 09.216Z,
    updatedAt: 2019-03-09T22
    :
    59: 09.216Z
  },
},
User {
  dataValues: {
    id: 16,
    user_id: '140235016357535420',
    server_id: '535881918483398676',
    xp: 40995,
    coins: 0,
    createdAt: 2019-03-09T22
    :
    59: 09.216Z,
    updatedAt: 2019-03-09T22
    :
    59: 09.216Z
  },
},]
Query
User.findAll({
    where: {
        server_id: msg.guild.id,
    },
    limit: 2,
    order: [
        ['xp', 'DESC'],
    ],
}).then(result => {
    console.log(result);
});
Model
'use strict';
module.exports = (sequelize, DataTypes) => {
    const User = sequelize.define('User', {
        user_id: DataTypes.STRING,
        server_id: DataTypes.STRING,
        xp: DataTypes.INTEGER,
        coins: DataTypes.INTEGER,
    }, {});
    /*  User.associate = function(models) {
        // associations can be defined here
    };*/
    return User;
};
                That looks like an array of User objects; dataValues is a child of each User. First iterate over the array and get the objects, then extract dataValues.
for (let i = 0; i < result.length; i++)  {
  console.log(result[i].dataValues);
}
Using forEach():
result.forEach( 
  (user) => { 
    console.log(user.dataValues);
  }
);
                        Before render/print you need to transform the Models array into array
const records = results.map(result => result.dataValues)
Details:
Out of Model search you've got an array of models like:
[ User {
  dataValues: {
    id: 16,
    user_id: '140235016357535420',
    server_id: '535881918483398676',
    xp: 40995,
    coins: 0,
    createdAt: 2019-03-09T22,
    updatedAt: 2019-03-09T22
  },
},
...]
Just "map" it like this:
const results = await User.findAll();
const records = results.map(function(result) {
            return result.dataValues
        })
and You'll get an array like this:
[
{
    id: 16,
    user_id: '140235016357535420',
    server_id: '535881918483398676',
    xp: 40995,
    coins: 0,
    createdAt: 2019-03-09T22,
    updatedAt: 2019-03-09T22
},...
]
*compact version:
const records = results.map(result => result.dataValues)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With