Doubt query SQL Nodejs

Asked

Viewed 22 times

0

I have the following question in my project for a college job: I need, in a specific route, to show the user’s data, which consists basically of his personal data and the posts he has performed. The problem is I’m not getting your personal information and your posts in an SQL query and show it all on a single page. Here’s what I did:

Table:

CREATE TABLE `claudioBlog`.`users` ( 
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT , 
  `username` VARCHAR(250) NOT NULL , 
  `email` VARCHAR(250) NOT NULL , 
  `discription` VARCHAR (250),
  `ocupation` VARCHAR (250),
  `profile_image` VARCHAR (250)
  `password` VARCHAR(255) NOT NULL , 
  PRIMARY KEY (`id`), UNIQUE (`email`)
  );
  
 CREATE TABLE posts (
  id int(100) NOT NULL AUTO_INCREMENT,
  title VARCHAR(250)  NOT NULL,
  category VARCHAR(250)  NOT NULL,
  body VARCHAR(250)  NOT NULL,
  writer_username VARCHAR(250)  NOT NULL,
  post_img VARCHAR(250),
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
 ) ;

Username was added as UNIQUE subsequently.

route/function/sql

router.get('/writer/:name', logginRequired, storyController.writerStories)

writerStories: (req, res) => {
        const name = req.params.name
        pool.getConnection((err, connection) => {
            connection.query('SELECT P.* FROM users U JOIN posts P on P.writer_username = U.username WHERE U.username=?',[name], (err, rows) => {
                connection.release()
                if(err) {
                    res.status(500).send(err)
                } else {
                    const row = rows
                    //row esta retornando []
                    res.render('writer', { data: row })
                }
            })
        })
    }

I need it to look like the photo below, and instead of the form will appear the posts user’s. inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.