4

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 3

4

Simply ommit the attributes key in your query and all the fields will return.

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

EDIT: In your model definition is best if you specify the fields your table has. Like so: http://docs.sequelizejs.com/manual/tutorial/models-definition.html

Otherwise you will have to specify the attributes key in every find operation.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for reply. I am begginer in nodejs. How to ommit the attribute key, give an example ?
when i am ommit the attribute then it return only id attribute.
Update your question and show us your model definition
Check my edited answer. You need to specify the fields in your model definition
2

@yBrodsky is right . Also you should make model like this.so that it can know the field to select when no attribute.

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

to this

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

const User = sequalize.define('user',{
 name: {type: Sequelize.STRING},
  email: {type: Sequelize.STRING},
  mobile: {type: Sequelize.INTEGER},
  dob: {type: Sequelize.DATE}
address: {type: Sequelize.STRING},
  image: {type: Sequelize.INTEGER},
  is_active: {type: Sequelize.BOOLEAN }


},{
    timestamps: false,
    paranoid: true,
    underscored: true,
    freezeTableName: true,
    tableName: 'user',
    createdAt:'created_on',
    updatedAt:'updated_on'
});

for dtatype see here http://docs.sequelizejs.com/manual/tutorial/models-definition.html#data-types

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.