使用knex查找数据Node js



我刚刚创建了一个使用googleoauth输入的函数。如果数据具有相同的谷歌用户id,则数据不应添加相同的谷歌用户名,但我创建的数据无法比较相同的数据。

这是我的功能:

// Check user
const existingUser = new User(database)
await existingUser.getUserGoogleId({ google_user_id: profile.id })
if (existingUser) {
// eslint-disable-next-line no-console
console.log('User already exists in our DB')
// eslint-disable-next-line no-console
console.log(existingUser)
return done(null, existingUser)
}

这是我的存储库:

async getUserGoogleId(google_user_id) {
const query = this.db
.select(
'id',
'google_user_id',
'username',
)
.from(TABLE_NAME)
.where({
google_user_id,
})
return query.first()
}

你们能告诉我,也许可以从中得到一些改进吗?谢谢。对不起,如果我有一个糟糕的解释

将对象传递给getUserGoogleId,然后在where子句中创建嵌套对象。

传递给where子句的对象看起来像:

{
google_user_id: {
google_user_id: 'some-id';
}
}

所有你需要解决的是传递对象本身:

async getUserGoogleId(google_user_id) {
return this.db
.select('id', 'google_user_id', 'username')
.from(TABLE_NAME)
.where(google_user_id)
// --------^
.first();
}

提示:在查询结束时启用.debug()以检查什么查询&params被传递到DB。

最新更新