gpt4 book ai didi

javascript - 使用 Knex.js 的数据加载器

转载 作者:行者123 更新时间:2023-12-02 22:30:16 25 4
gpt4 key购买 nike

在我更新到 dataloader: 2.0.0 之前,此代码工作正常

const productLoader = new DataLoader(async keys => {
const products: Product[] = await knex('product')
.whereIn('id', keys)
.select()

const productMap: any = {}

products.forEach((p: any) => {
productMap[p.id] = p
})

return keys.map((k: any) => productMap[k])
})

export default productLoader

现在它给出错误:

loader.ts:7:14 - error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type '"id"' is not assignable to parameter of type 'string[]'.

7 .whereIn('id', keys)
~~~~

node_modules/knex/types/index.d.ts:1137:5
1137 <TRecordInner, TResultInner>(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1138 columnNames: string[],
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1139 values: QueryBuilder<TRecordInner, TResultInner>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1140 ): QueryBuilder<TRecord, TResult>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The last overload is declared here.


Found 1 error.

我做错了什么?

// package.json

"dataloader": "^2.0.0",
"knex": "^0.20.2",

堆栈:Apollo-server-express、TypeScript、Postgres、Knex.js

最佳答案

knex (TypeScript) 似乎希望您将字符串数组 (string[]) 设置为 whereIn 的第一个参数,如下所示:

const products: Product[] = await knex('product')
.whereIn(['id'], keys)
.select();

这与在多列中搜索时相同(以下示例来自 Knex.js 文档):

knex.select('name').from('users')   
.whereIn(['account_id', 'email'], [[3, 'test3@example.com'], [4, 'test4@example.com']])

希望有帮助,
最好的问候

关于javascript - 使用 Knex.js 的数据加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58928171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com