gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:01:56 25 4
gpt4 key购买 nike

我编写了以下代码来检查我的数据库 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com