javascript - 检查用户是否在 node.js 中的 postgresql 中(异步)

标签 javascript node.js postgresql

我编写了以下代码来检查我的数据库 (PostgreSQL) 中是否存在用户,我使用该 ID 来检查我的终端,它确实找到了具有我获得的 ID 的用户。但是这里显示 userAuthentication 函数中 checkUser 的结果是 undefined。 有谁能帮我这个吗?谢谢!

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

}

最佳答案

您应该从 checkUser 函数返回一个 promise :

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

我没有自己检查这个解决方案,请检查。从逻辑上讲,它应该有效。

关于javascript - 检查用户是否在 node.js 中的 postgresql 中(异步),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62436754/

相关文章:

postgresql - 记录设置为 SECURITY DEFINER 的 Postgres 函数的调用程序

javascript - 如何使用 javascript 将上传的照片保存到我的 HTML 项目

使用 JAX-RS 进行 JavaScript cookie 管理

javascript - 当子组件中发出事件时,父组件中的对象未接收

javascript - 将不同的 DOM 元素隐藏到通过 jquery 过滤的元素

javascript - 如何在 Javascript 中执行不同的基数对数函数?

javascript - 如何在sendgrid中发送带有.xlsx附件的电子邮件?

node.js - 检查用户是否通过 Passport 策略登录(不同用户类型)

sql - psql query CASE vs Multiple select for a large data-sets - performance

sql - 如何使用 TypeORM 创建这个 ViewEntity?