[英]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.