我想获取所有字段而不写表的所有字段名称。如何使用*来从表中获取所有字段?
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;
请帮助我了解如何通过控制器中的写入属性来获取所有字段。
答案 0 :(得分:4)
只需在查询中省略属性键,所有字段都将返回。
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);
});
编辑:如果您指定表格中的字段,最好在模型定义中。像这样:http://docs.sequelizejs.com/manual/tutorial/models-definition.html
否则,您必须在每个查找操作中指定属性键。
答案 1 :(得分:1)
@yBrodsky是对的。你也应该像这样制作模型。它可以知道在没有属性时选择的字段。
const User = sequalize.define('user',{},{
timestamps: false,
paranoid: true,
underscored: true,
freezeTableName: true,
tableName: 'user',
createdAt:'created_on',
updatedAt:'updated_on'
});
到这个
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'
});
对于dtatype,请看这里 http://docs.sequelizejs.com/manual/tutorial/models-definition.html#data-types
答案 2 :(得分:0)
只需从模型中删除属性,即可提取所有列