Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if user is in postgresql in node.js (async)

I wrote the following code to check if a user exists in my database (PostgreSQL) and I used that id to check in my terminal and it does found the user with that id I got. However here it showed me that the result of checkUser within the userAuthentication function is undefined. Does anyone can help me with this? Thanks!

function checkUser(userId){
  let SQL = 'SELECT * FROM Users WHERE github_id=$1;';
  console.log('this is the userid from checkUser', userId);
  let values = [userId];
  client.query(SQL,values)
    .then(result => {
      console.log('here is the user from SQL',result.rows[0]);
      return result.rows[0];
    })
    .catch(err => {console.log(err)});
}

function userAuthentication(user_data, token){
  return checkUser(user_data.id)
    .then(user => {
      console.log('within user authentication user from checking', user);
      if(user !== undefined){
        console.log('xxxx have user within authentication', user.token);
        let SQL = 'UPDATE Users SET token = $1 WHERE token=$2;';
        let values = [token, user.token];
        client.query(SQL,values)
          .then(result => {
            console.log('here is the user with new token',result);
            return result;
          })
          .catch(err => {console.log(err)});
      }else{
        console.log('xxx get in creating new user');
        createUser(user_data,token);
      }
    })
    .catch(err => {console.log(`Something wrong with userAuthentication ${err}`)});
}


function createUser(user_data,user_token) {
  const newUser = new User({
    token: user_token,
    github_username: user_data.login,
    github_id: user_data.id,
    github_url: user_data.url,
    avatar_url: user_data.avatar_url,
    gravatar_url: user_data.gravatar_id
  });
  console.log('XXXX this is the new user created', newUser);

  // save user to sql
  let SQL = 'INSERT INTO users (token, experience_lvl, position, github_username, github_id, github_url, avatar_url, gravatar_url, last_login, is_superuser, username, first_name, last_name, email, is_active, date_joined) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING id;';
  let values = [newUser.token, newUser.experience_lvl, newUser.position, newUser.github_username, newUser.github_id, newUser.github_url, newUser.avatar_url, newUser.gravatar_url, newUser.last_login, newUser.is_superuser, newUser.username, newUser.first_name, newUser.last_name, newUser.email, newUser.is_active, newUser.date_joined];

  // console.log("the query", SQL);
  client.query(SQL, values)
    .then(result => console.log('XXXX got in sql saving', result))
    .catch(err => console.log(err));

}
like image 533
Leylali Avatar asked Apr 13 '26 02:04

Leylali


1 Answers

You should return a promise from the checkUser function:

function checkUser(userId){
    const SQL = 'SELECT * FROM Users WHERE github_id=$1;';
    console.log('this is the userid from checkUser', userId);
    const values = [userId];
    return client.query(SQL,values)
      .then(result => {
        console.log('here is the user from SQL', result.rows[0]);
        return result.rows[0];
      })
      .catch(err => {console.log(err)});
}

I did not check this solution myself, please check. Logically, it should work.

like image 161
Rustam D9RS Avatar answered Apr 15 '26 16:04

Rustam D9RS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!