使用knex查找数据节点js

Find data node js using knex

我刚刚创建了一个使用 google oauth 进入的函数。如果数据有相同的google用户id,则数据不应添加相同的google用户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() 以检查传递给数据库的查询和参数。