繁体   English   中英

带有 Knex.js 的数据加载器

[英]Dataloader with Knex.js

在我更新到数据加载器之前,这段代码运行良好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']])

希望能帮助到你,
此致

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM