我有两个表 users 和 accounts ( users.id = accounts.id ),我想进行以下登录:从这两个表,users.username或accounts.mail中访问
我正在使用BookShelf JS,并且我已经尝试过各种方法来确定users.username =标识是否不存在,然后检查account.mail =标识并且它们都不起作用
//This code obtains the desired result but only when given the username.
return UserModel.query((qb) => {
qb.where('username', identification)
//.orWhere('account.mail', identification) Something Like This!
})
.fetch({
withRelated: [{ 'account': (qb) => { qb.column('id', 'mail', 'password') } }],
columns: ['id', 'username']
})
因此,如果给定users.username,那么将给定users.id和account.id,如果我给了account.mail,那么还将给定account.id和users.id。
答案 0 :(得分:0)
我找到了一种替代方法,但是它可以工作,如果有人有更好的方法,我愿意接受建议:
return new Promise((resolve, reject) =>
{
if(identification == null || password == null) return reject(new Error('invalid_parameters'));
return UserModel.query((qb) => {
qb.where('username', identification)
})
.fetch({
withRelated: [{ 'account': (qb) => { qb.column('id', 'mail', 'password') } }],
columns: ['id', 'username']
})
.then((result) =>
{
if(result != null)
{
result = result.toJSON();
return resolve({id: result.id, username: result.username});
}
return AccountModel.query((qb) => {
qb.where('mail', identification)
})
.fetch({
withRelated: [{ 'user': (qb) => { qb.column('id', 'username') } }],
columns: ['id', 'mail', 'password']
})
})
.then((result) =>
{
if(result == null) return reject(new Error('invalid_login'));
result = result.toJSON();
return resolve({id: result.id, username: result.user.username});
})
.catch((err) =>
{
return reject(err);
});
});
我所做的只是检查结果是否不为null,如果不为null,则使用正确的凭据进行解析,否则,如果result为null,则检查另一个模型(AccountModel),如果也为null,然后拒绝,否则,请使用正确的凭据进行解决
答案 1 :(得分:0)
您可以尝试以这种方式实现连接。
[1]