【发布时间】:2020-02-27 22:29:26
【问题描述】:
我正在尝试通过简单的登录来验证用户。我在我的项目中使用 node.js 和 mysql,我正在做的是检查电子邮件是否存在于字段电子邮件和我的帐户表中。我将查询结果传递给一个常量行,然后检查它的长度,如果它返回大于 0,则意味着它找到了一些东西(?),这是我的 sn-p:
console.log(testEmail) //it does receives an email
console.log(password) //it does receives a password
const rows = await pool.query('SELECT * FROM accounts WHERE email = ?', [testEmail.email]);
console.log(rows); //row comes out empty
这里是完整的方法:
passport.use('local.signin', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, async(req, username, password, done) => {
const {email} = req.body;
const testEmail = {
email
};
console.log(testEmail)
console.log(password)
const rows = await pool.query('SELECT * FROM accounts WHERE email = ?', [testEmail.email]);
console.log(rows);
if (rows.length > 0) {
const user = rows[0];
const validPassword = await helpers.matchPassword(password, user.password);
if(validPassword){
done(null, user, req.flash('success', 'Welcome!'));
} else{
done(null, false, req.flash('message', 'Incorrect password'));
}
} else{
return done(null, false, req.flash('message', 'Email not found'));
}
}));
添加邮箱和密码的控制台结果:
{ email: 'irwin.inoa@gmail.com' }
123456
添加我的表定义:
`id` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`password` char(60) NOT NULL,
`email` varchar(100) NOT NULL
关于如何解决这个问题的任何想法,我错过了什么吗?
更新:
如下所述,通过对象 testEmail 传递 email 属性可以解决此问题。
testEmail.email.
感谢您的宝贵时间
【问题讨论】:
-
可能像
(async function(){ const rows = await pool.query('SELECT * FROM accounts WHERE email = ?', [testEmail]); console.log(rows); })();这样的 IIFE 会有所帮助。 -
``` passport.use('local.signin', new LocalStrategy({ usernameField: 'email', passwordField: 'password', passReqToCallback: true }, async(req, username, password, done) => { const {email} = req.body; const testEmail = { email }; console.log(testEmail) console.log(password) const rows = await pool.query('SELECT * FROM accounts WHERE email = ? ', [testEmail]); console.log(rows); if (rows.length > 0) { ```
标签: javascript mysql sql node.js