I have a models listed as in below:
const sequelize =require ('./db')
const UserInfo = sequelize.define('user',{
userId : {type: DataTypes.INTEGER, primaryKey: true, allowNull: false},
email: {type: DataTypes.STRING, unique: true, allowNull: false},
password: {type: DataTypes.STRING, allowNull: false}
})
const User = sequelize.define('user_info',{
id : {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false},
userName : {type: DataTypes.STRING, unique: true, allowNull: false}
})
User.hasOne(UserInfo)
UserInfo.belongsTo(User)
module.exports = {User, UserInfo}
and I want to fetch UserInfo data so that I can get data from his parental model (User) as well, especially userName
async login (req, res) {
let {email, password} = req.body
const user = await UserInfo.findOne({
where: {email, password},
include: [{model: User, as : 'user'}],
})
return res.json(user)
}
So the result should look like this:
{
email: 'someemail@gmail.com',
password: 'somepassword', //(this data is listed in child UserInfo model)
userName: 'user1234' //(this data is listed in parental User model)
}