我需要查询Node.js项目中的两个表。该项目使用Sequelize和Mysql。
当用户在“postagens table”中注册一个帖子时,他会记录该电子邮件。当访问视图时,我需要在“users table”中查询用户的全名,并将其加载到视图中。
<td> {{fullname}} </td>我应该在'app.js‘中做什么更改?
非常感谢!
//app.js
app.get('/', function (req, res) {
Post.findAll({ raw: true, where: {
companyid: '3',
enviado: '0'
} }).then(function (posts) {
res.render('home', { posts: posts });
});
});//home.handlebar
<div class="card">
<div class = "card-body">
<h1>Lista de Posts</h1>
<form class="new-post" action="/cad" method="get">
<button class="btn btn-success" type="submit">Nova Postagem!</button>
</form>
<table class="table" width= 100%>
<thead>
<td>id</td>
<td>Título</td>
<td>Conteúdo</td>
<td>Fullname</td>
<td>Ações</td>
</thead>
<tbody>
{{#each posts}}
<tr>
<td>{{id}}</td>
<td>{{titulo}}</td>
<td>{{conteudo}}</td>
<td>{{fullname}}</td>
<td>
<form class="form-action" action="/edit/{{id}}" method="get">
<button class="btn btn-success" type="submit">edit</button>
</form>
</td>
<td>
<form class="form-action" action="/deletar/{{id}}" method="get">
<button class="btn btn-success" type="submit">delete</button>
</form>
</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
</div>//table postagens
CREATE TABLE `postagens` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`titulo` VARCHAR(255) NULL DEFAULT NULL COLLATE 'latin1_general_ci',
`conteudo` TEXT NULL COLLATE 'latin1_general_ci',
`companyid` TEXT NULL COLLATE 'latin1_general_ci',
`enviado` TEXT NULL COLLATE 'latin1_general_ci',
`email` TEXT NULL COLLATE 'latin1_general_ci',
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=51
;//表用户
CREATE TABLE `users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`password` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`fullname` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=4
;发布于 2020-02-24 10:50:11
您需要调用users.findOne或users.findByPk,并且需要在请求中传递要显示的用户的id字段……然而,至于你的应用程序文件的更改,我不太确定我真的知道那里发生了什么……你总是让它调用一个查询....如果你的节点服务器上有像apollo graphql这样的东西,那么你只需要传递一个查询...您需要一些方法来表明您希望调用diff sequelize查询
https://stackoverflow.com/questions/60368282
复制相似问题