我一直在尝试将pg-promise与passwordjs一起使用,但是我无法进行本地注册。我的登录正常。请看下面:
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);
});
});
}
)
);
这里的问题是,它实际上检测到用户是否已经在数据库中,但是如果该用户不存在,则不会创建用户名,而是跳过整个过程。
有什么办法解决此问题吗?
谢谢
答案 0 :(得分:1)
您在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);
});