使用knex减量与async / await

时间:2018-02-23 11:42:01

标签: javascript postgresql async-await knex.js decrement

我在尝试使用knex .decrement() withg async / await时遇到错误:

  const test = await new CollectionGroup()
    .where('uuid', '=', asset_collection_group_uuid)
    .decrement('item_out_quantity', 1);

  console.log('TEST', test);

输出:

  sql: 'select "asset_collection_item".* from "asset_collection_item" where "asset_collection_item"."uuid" = ? limit ?' }
err   TypeError: (intermediate value).where(...).decrement is not a function

  - index.js:182 Object.deleteItem
    D:/work/project/src/svc/borrow/item/index.js:182:10

如果我删除.decrement()

,我就不会收到错误消息
  const test = await new CollectionGroup()
    .where('uuid', '=', asset_collection_group_uuid)

将knex .decrement()与async / await一起使用的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

这就是我解决它的方法:

  const test = await CollectionGroup.query()
    .where('uuid', asset_collection_group_uuid)
    .decrement('item_out_quantity', 1);

    console.log('TEST', test);

输出:

{ method: 'counter',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [ '49b7a731-7910-4e9d-9bb2-a27f9f8c210d' ],
  __knexQueryUid: '67ccb2b8-519f-4fea-aab5-c4c287d0d699',
  toNative: [Function: toNative],
  sql: 'update "asset_collection_group" set "item_out_quantity" = "item_out_quantity" - 1 where "uuid" = ?',
  returning: undefined }
TEST 1