简体   繁体   English

我不明白为什么我在使用 ts-jest 和 sequelize 时遇到此错误

[英]I don't understand why i am getting this error with ts-jest and sequelize

I'm working on an API using nodeJs, TypeScript, Sequelize and jest (ts-jest) and there is an error i'm sure to understand.我正在使用 nodeJs、TypeScript、Sequelize 和 jest (ts-jest) 开发 API,我肯定会理解错误。

i have the follwing code:我有以下代码:

router.get('/hotels', async (req, res) => {
 try {
  const hotels = await Hotel.findAll({
    include: [
      {
        model: Visit,
      },
      {
        model: Sector,
      },
    ],
    ...(req.query.limit && { limit: req.query.limit }),
    order: [
      ['name', 'ASC'],
      [Visit, 'date', 'ASC NULLS FIRST'],
    ],
  });
  res.send(hotels);
} catch (error) {
  res.status(404).send({ error });
}

}); });

but when i run the command 'jest' it show me this error:但是当我运行命令'jest'时,它显示了这个错误:

 app/routes/hotel.ts:33:40 - error TS2345: Argument of type '{ order: ([string, string] | [typeof Visit, string, string])[]; include: ({ model: typeof Visit; } | { model: typeof Sector; })[]; } | { order: ([string, string] | [typeof Visit, string, string])[]; include: ({ ...; } | { ...; })[]; } | { ...; }' is not assignable to parameter of type 'FindOptions | undefined'.
  Type '{ order: ([string, string] | [typeof Visit, string, string])[]; limit: string | QueryString.ParsedQs | string[] | QueryString.ParsedQs[]; include: ({ model: typeof Visit; } | { ...; })[]; }' is not assignable to type 'FindOptions'.
    Types of property 'limit' are incompatible.
      Type 'string | ParsedQs | string[] | ParsedQs[]' is not assignable to type 'number | undefined'.
        Type 'string' is not assignable to type 'number | undefined'.

 33     const hotels = await Hotel.findAll({
                                           ~
 34       include: [
    ~~~~~~~~~~~~~~~~
... 
 46       ],
    ~~~~~~~~
 47     });
    ~~~~~

it think there is a problem with types that ts-jest seem to not understand.它认为 ts-jest 似乎不理解的类型存在问题。 I can provide more details if you ask me.如果你问我,我可以提供更多细节。

I'm using jest ^25.5.4, ts-jest^25.5.1 and Sequelize我正在使用 jest ^25.5.4、ts-jest^25.5.1 和 Sequelize

Any help will be much appreciated.任何帮助都感激不尽。 Thanks in advance for your time.在此先感谢您的时间。

Maxime马克西姆

I'm not familiar with the stack, but it looks like it knows that the type of req.query.limit is a string, and the methods expects the limit prop to be a number.我对堆栈不熟悉,但看起来它知道req.query.limit的类型是一个字符串,并且这些方法期望 limit 属性是一个数字。

if you are sure that req.query.limit is a number you can probably replace:如果您确定req.query.limit是一个可以替换的数字:

...(req.query.limit && { limit: req.query.limit }),

with

{limit: req.query.limit? Number(req.query.limit): null}

or do more advanced parses to convert limit query param to number as you see fit或进行更高级的解析以将限制查询参数转换为您认为合适的数字

暂无
暂无

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

相关问题 在使用 jest/ts-jest 模拟打字稿中的类时,我收到“TypeError: "X".default is not a constructor。” - When mocking a class in typescript using jest/ts-jest I am getting "TypeError: "X".default is not a constructor." Typescript - 我无法使用 jest、@types/jest 和 ts-jest 编译项目 - Typescript - I can't compile project with jest, @types/jest and ts-jest 当我使用jest(ts-jest异步/等待测试)时,WebStorm提示我TS错误(TS2705) - WebStorm hints me the TS error (TS2705) when I use jest(ts-jest async/await test) 使用ts-jest,得到`错误TS2554:预期的0个参数,但是为构造函数得到1` - Using ts-jest, getting `error TS2554: Expected 0 arguments, but got 1` for constructor 为什么我会收到这样的错误? 错误 TS2564: - Why am I getting such an error? error TS2564: 我无法编译 TypeScript 错误 TS1005: ',' 预期,但不知道为什么 - I am getting unable to compile TypeScript error TS1005: ',' expected, but can't figure out why 为什么我收到错误 ts(1005) ',' 预期 - Why am I getting error ts(1005) ',' expected 我正在写一个开玩笑的快照测试,该组件使用的是 redux,为什么它给我 TS 错误, - I am writing a jest snapshot testing, the component is using the redux, why it is giving me TS error, 如何使用 ts-jest 解析 next.js 中的别名组件? - how can I resolve alias components in next.js with ts-jest? 如何使用当前的 ts-jest 获取覆盖图? - How can I get coverage maps using current ts-jest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM