3

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

}

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.