0
votes

I am currently making a webapp with a database using express and postgres and am having trouble implementing registration. This is a small snippet of what happens after the user keys in username, name and password into a form which is then posted to the '/registerUser' route. Here is the post request at the '/registerUser' route. The pool.query works for the check_username, however, is does not work for the add_user query.

Here are the 2 queries: check_username = 'SELECT 1 FROM Users where username = $1' add_user = 'INSERT INTO users VALUES (username, name, password) VALUES ($1, $2, $3)'

router.post('/', function(req, res, next) {
    var name = req.body.name;
    var username = req.body.username;
    var password = req.body.password;

    pool.query(sql_query.query.check_username, [username], (err, data) =>{
        if(data.rows[0] != undefined) {
      console.log(`re register again!`);
            res.redirect('/registerUser');
        } else {
      console.log("adding to database")
        // Generate salt
      bcrypt.genSalt(10, (err, salt) =>{
        if(err) {
          console.log(err);
        }
        // hash it with salt
        bcrypt.hash(password, salt, (err,hash)=>{
          if(err) console.log(err)
          //password = hash
          console.log(`${username} + ${password}`);
          // save to database 
          pool.query(sql_query.query.add_user, [name, username, password], (err, data) => {
            console.log("pass")
            res.redirect("/")
          });
        });
      });
    }
  });
});

What exactly is the problem here? Could it be that i cannot nest pool.queries?

1

1 Answers

0
votes

the answer is that there is a syntax error with the insert queries. There shouldnt be a VALUES in front of the table columns.