我正在开发一个节点js应用程序,并使用DB作为mysql,我想做的是当我运行查询并提取所有数据时,我想访问该数据或将该数据存储到变量中以备将来使用
在我的控制器中,我正在编写此代码
exports.login = function(req, res) {
User.fetchUser()
.then(([rows]) => {
console.log(rows)
})
.catch(err => console.log(err));
}
这是在控制台上打印 [ BinaryRow { email: '[email protected]', password: 'dheeraj' } ]
在我的模型课中,我正在执行我的fetchUser
功能
static fetchUser() {
const email = '[email protected]'
const password = 'dheeraj'
let sql = 'SELECT email,password FROM tpconsumer where email = ? and password = ?'
return db.execute(sql, [email, password]);
}
现在我想做的就是获取电子邮件和密码值,并将它们存储在变量中以备将来使用,或者只是我该如何使用电子邮件或密码来访问它们
尝试将参数传递给fetchUser方法
exports.login = function(req, res) {
User.fetchUser(email,password)
.then(([rows]) => {
if(rows.length >0)
{
for(var i=0; i<rows.length; i++){
console.log(rows[i].email);
console.log(rows[i].password);
}
}
else{
console.log('Nothing to fetch');
}
})
.catch(err => console.log(err));
在您的班级模型中:
static fetchUser(email,password) {
/*const email = '[email protected]'
const password = 'dheeraj'*/
//pass your data dynamically
let sql = 'SELECT email,password FROM tpconsumer where email = ? and password = ?'
return db.execute(sql, [email, password]);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句