Javascript 使用NODE.js在MySQL数据库上查找电子邮件
我试图用一个简单的登录来验证一个用户。我在我的项目中使用node.js和mysql,我所做的是检查字段email y my accounts表中是否存在电子邮件。我将查询结果传递给一个常量行,然后检查其长度,如果它返回的值大于0,则表示它找到了某个内容(?),下面是我的代码片段:Javascript 使用NODE.js在MySQL数据库上查找电子邮件,javascript,mysql,sql,node.js,Javascript,Mysql,Sql,Node.js,我试图用一个简单的登录来验证一个用户。我在我的项目中使用node.js和mysql,我所做的是检查字段email y my accounts表中是否存在电子邮件。我将查询结果传递给一个常量行,然后检查其长度,如果它返回的值大于0,则表示它找到了某个内容(?),下面是我的代码片段: console.log(testEmail) //it does receives an email console.log(password) //it does receives a password const
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.
感谢您抽出时间原始答案
正如我在中看到的,pool.query()
函数具有回调或返回承诺。您可以获得如下结果:
//回调
pool.query('SELECT*FROM accounts WHERE email=?',[testEmail],(err,rows)=>{
如果(错误){
返回console.error('error executing query',err.stack');
}
console.log(行);
});
//允诺
查询('SELECT*FROM accounts WHERE email=?',[testEmail])
。然后(行=>{
返回console.log(行);
})
.catch(错误=>{
返回console.error('error executing query',err.stack');
});
编辑
好的,为了进行测试,请尝试下面的代码,看看表是否正确。可能我们没有正确地将参数传递给查询
//返回表的所有记录
查询('SELECT*FROM accounts LIMIT 100',[],(err,rows)=>{
如果(错误){
返回console.error('error executing query',err.stack');
}
console.log(行);
});
//传递参数的另一种方法
pool.query('SELECT*FROM accounts WHERE email=$1::text',[testEmail],(err,rows)=>{
如果(错误){
返回console.error('error executing query',err.stack');
}
console.log(行);
});
您的查询可能有错误,但由于您没有尝试/捕获您的查询,因此您不知道发生了什么。您可能希望执行以下操作:
let rows = [];
try{
rows = await pool.query('SELECT * FROM accounts WHERE email = ?', [testEmail.email]);
}catch(err){
console.log("Query Error", err);
}
可能是像
(async function(){const rows=wait pool.query('SELECT*FROM accounts WHERE email=?',[testEmail]);console.log(rows);})这样的IIFE代码>可能有帮助。``passport.use('local.signin',新的LocalStrategy({usernameField:'email',passwordField:'password',passReqToCallback:true}),async(req,username,password,done)=>{const{email}=req.body;const testEmail={email};console.log(testEmail)console.log(密码)const rows=wait pool.query('SELECT*FROM accounts WHERE email=?',[testEmail]);console.log(rows);if(rows.length>0){```您好!我尝试了这个方法,results.rows返回了未定义的内容关于打印结果
,它会返回什么?我得到这个:[]您在账户
表上有任何记录吗?您传递的testEmail
表中有相关记录吗?是的,先生,这些是mytable属性:id
int(11)NOT NULL,username
varchar(50)NOT NULL,password
char(60)NOT NULL,email
varchar(100)NOT NULLReturns undefinedOkay我用let rows=[]尝试过,它仍然返回空的,它是否与它的存储方式有关?它显示了我在终端中传递的电子邮件,如下所示:{email:'irwin。inoa@gmail.com' }
哦,现在可以看到完整的代码了。我发现您将电子邮件存储在一个对象中。因此在查询中,您需要使用[testEmail.email]