How to fetch all fields of table using sequelize in nodejs?

Viewed 14067

I want to fetch all fields without to write all fields name of table. How to use * to fetch all the fields from the table ?

This is UserController function to fetch all the rows with all fields of table.

user.findAll({
        attributes: ['name','email','mobile','dob','address','image','is_active'],
        where:{
            is_active:'1',
            name:{$like:'%%'}
        },
        limit:10
    }).then(function(users,fields){
        console.log(users);
        res.send({error:false,message:'users list',data:users});
    }).catch(function(err){
        console.log('Oops! something went wrong, : ', err);
    });

This is model code and define some property of model.

var sequalize = require('../../config/db_config');

const User = sequalize.define('user',{},{
    timestamps: false,
    paranoid: true,
    underscored: true,
    freezeTableName: true,
    tableName: 'user',
    createdAt:'created_on',
    updatedAt:'updated_on'
});

User.sync({force:false}).then(() => {
    console.log('Table is created!');    
}).catch(err => {
    console.log('An error occur when table is created!');
});
module.exports = User;

Please help me that how to fetch all fields with write to attribute in controller.

3 Answers

Simply remove attributes from model , you will be able fetch all columns

Related