[英]How to get all username of model in sequelize
我正在使用Express和Javascript使用Docker開發 Web 應用程序。 我正在使用三層架構並使用Sequelize使用Mysql發送查詢。 我有博客文章作為與帳戶表有關系的資源。 這是關系的樣子:
const Account = sequelize.define('accounts', {
personId: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
username: {
type: Sequelize.STRING(50),
allowNull: false,
unique: true
},
email: {
type: Sequelize.STRING(50),
allowNull: false,
unique: true
},
userPassword: Sequelize.TEXT
}, {
timestamps: false
})
const Blogpost = sequelize.define('blogposts', {
blogId: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: Sequelize.TEXT,
content: Sequelize.TEXT,
posted: Sequelize.TEXT,
imageFile: Sequelize.TEXT
}, {
timestamps: false
})
Blogpost.belongsTo(Account, {foreignKey: "userId", foreignKeyConstraint: true})
這就是我在表示層中檢索所有博客文章的方式:
router.get("/", function(request, response){
blogManager.getAllBlogposts(function(errors, blogposts){
if(errors.includes("databaseError")){
response.send("<h1>Something went wrong!</h1>")
}
else if(errors.length > 0){
const model = {
errors
}
console.log("blogpostsModel:", { model })
response.render("blogposts.hbs", { model })
}else{
const model = {
blogposts
}
response.render("blogposts.hbs", { model })
}
})
})
這就是模型的樣子:
blogposts {
dataValues: {
blogId: 2,
title: 'fsdhkjfsdahflhs',
content: 'fhjkdsfkbmngfhyuxgc',
posted: '275760-07-09',
imageFile: 'module-6.jpg',
userId: 1
},
_previousDataValues: {
blogId: 2,
title: 'fsdhkjfsdahflhs',
content: 'fhjkdsfkbmngfhyuxgc',
posted: '275760-07-09',
imageFile: 'module-6.jpg',
userId: 1
},
blogposts {
dataValues: {
blogId: 3,
title: 'fjhhkjfsa',
content: 'gfhjdsakdasdsa',
posted: '8989-08-09',
imageFile: 'module-6.jpg',
userId: 2
},
_previousDataValues: {
blogId: 3,
title: 'fjhhkjfsa',
content: 'gfhjdsakdasdsa',
posted: '8989-08-09',
imageFile: 'module-6.jpg',
userId: 2
},
現在我想用用戶 ID 檢索用戶名。 我有一個可以通過 userid 檢索用戶名的工作函數,但我不知道如何使用它從 userid 中獲取所有用戶名。 任何想法如何解決這個問題?
提前致謝!
您可以在查詢blogposts
使用include
選項,如下所示
Blogpost.findAll({
include: [{
model:Account,
}]
});
它將使用 join 查詢來獲取account
但如果您想使用單獨的查詢,那么您也可以執行以下操作
router.get("/", function(request, response){
blogManager.getAllBlogposts(function(errors, blogposts){
if(errors.includes("databaseError")){
response.send("<h1>Something went wrong!</h1>")
}
else if(errors.length > 0){
const model = {
errors
}
console.log("blogpostsModel:", { model })
response.render("blogposts.hbs", { model })
}else{
const userIds = blogposts.map(blogpost =>
blogpost.userId);
// query the Account with the userIds;
Account.find({ where : { userId : userIds}})
.then((accounts) => {
const blogpostsWithUserNames =
blogposts.map(blogpost => {
const findAccount =
accounts.filter(acc =>
acc.userId === blogpost.userId);
return {
...blogpost,
userName : findAccount.username,
}
})
const model = {
blogposts : blogpostsWithUserNames
}
response.render("blogposts.hbs",
{ model })
});
}
})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.