简体   繁体   中英

Query Node.js , Sequelize and Mysql

I need to query two tables in a Node.js project. The project uses Sequelize and Mysql.

When a user registers a post in 'postagens table', he records the email. When accessing the view, I need to consult the user's full name in 'users table' and load it into the view.

<td> {{fullname}} </td>

What change should I make in 'app.js'?

Very thanks!

//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.handlebars

    <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
;

//table users

   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
;

您需要调用 users.findOne 或 users.findByPk 并且您需要传递要在请求中显示的用户的 id 字段...但是至于对您的应用程序文件的更改,我不太确定我知道那里发生了什么......你总是让它调用一个查询......如果你的节点服务器上有类似 apollo graphql 的东西,那么你只需传递一个查询......你需要某种方式来向你展示想要调用 diff sequelize 查询

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM