我是风帆的新手,我正在制作一个简单的登录功能,其中名为 find 的 Controller 将转到数据库并检查用户名密码是否有效,但我面临的问题是它正在检查用户名和我不要考虑密码,因为在参数中我传递了正确的密码,我只是使用链接传递参数,我传递参数的链接是
http://localhost:1337/find/index?name=ahsan&pass=pakistan
我的 find Controller 中的代码是
var Sequelize = require('sequelize');
var sequelize = require('../../config/config_db').dbase;
var user = sequelize.define('Data',{
username: Sequelize.STRING,
birthday: Sequelize.STRING
})
sequelize.sync();
module.exports = {
index: function (req, res) {
var n = req.param('name');
var p = req.param('pass');
user.find({ where: { username : n, password : p } })
.complete(function(err, name, pass)
{
if (!!err)
{
return res.json('error while connecting db')
}
else if (!name)
{
return res.json('no user with this name')
}
else if (!pass)
{
return res.json('no user with this password')
}
else
{
return res.json('user is valid')
};
})
// Send a JSON response
// return res.json({
// hello: 'world'
},
_config: {}
};
在控制台中执行的查询是
SELECT * FROM `Data` WHERE `Data`.`username`='ahsan' AND `Data`.`password`='pakistan' LIMIT1;
密码巴基斯坦存在于我的数据库中,它显示给我的是
"no user with this password"
如果我弄错了,请告诉我。
最佳答案
您在 Sequelize 中滥用了 .complete()
回调。它应该有两个参数:err
和检索到的项目(在本例中为用户)。尝试:
user.find({ where: { username : n, password : p } })
.complete(function(err, user)
{
if (!!err)
{
return res.json('error while connecting db')
}
else if (!user.name)
{
return res.json('no user with this name')
}
else if (!user.pass)
{
return res.json('no user with this password')
}
else
{
return res.json('user is valid')
};
});
关于node.js - 与风 sails Controller 中的密码参数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23962806/