javascript - ตัวโหลดข้อมูลด้วย 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
คำตอบ #1
ดูเหมือนว่า 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, '[email '], [4, '[email ']])
หวังว่าจะช่วยได้,
ด้วยความนับถือ
