node.js - PassportJS 和使用 postgres 创建用户
问题描述
我一直在尝试将 pg-promise 与 passportjs 一起使用,但我无法进行本地注册。我的登录效果很好。请看下面:
passport.use(
"local-signup",
new LocalStrategy(
{
usernameField: "email",
passwordField: "pass",
passReqToCallback: true
},
(req, email, pass, done) => {
process.nextTick(function() {
q.db
.one("select email from users where email = $1", email)
.then(data => {
if (data) {
return done(
null,
false,
req.flash("signupMessage", "User already exists")
);
} else {
console.log("here!?");
q.db
.none(
"insert into users (email, pass)" +
`values (${email}, ${pass})`
)
.then(data => {
console.log("created?");
});
}
})
.catch(err => {
console.log(err);
});
});
}
)
);
这里的问题是,它实际上检测用户是否已经在数据库中,但如果用户不存在,它不会创建用户名,而是跳过整个过程。
任何想法如何解决这个问题?
谢谢
解决方案
你里面的代码nextTick
应该是这样的:
q.db.task(async t => {
const user = await t.oneOrNone('SELECT * FROM users WHERE email = $1', email);
if (user) {
return false;
}
await t.none('INSERT INTO users(email, pass) VALUES($1, $2)', [email, pass]);
return true;
})
.then(created => {
done(
null,
false,
req.flash('signupMessage', 'Created: ' + created)
);
})
.catch(error => {
console.log(error);
});
推荐阅读
- kotlin - 更改高度和颜色选项卡指示器
- java - Tomcat重写url给出404,而直接浏览给出200
- javascript - 如何使 div 标签与 state(react.js) 中的数字一样多
- excel - 当工作表处于非活动状态或窗口最小化时,无法设置 OLEObjects 属性
- python - Pybind11 Wrapper通过引用传入时非法调用非静态成员函数
- c# - 获取列表
不在列表中 - >
- python - 使用硒数据发布请求
- mysql - Laravel 在几个条件下使用 sum
- r - 如何粘贴两个带有条件的列字符串
- firebase - 不变违规:组件`Friends`的视图配置getter回调必须是一个函数(收到`undefined`)