我将 Sequelize findAndCountAll()
用于我网站的分页。findAndCountAll()
返回的总项目数( 3990
)大于实际返回的数据数( 3770
)。
有谁知道如何使 findAndCountAll()
返回与返回的实际数据相同的总项目数?
我使用 findAndCountAll()
查询的表包括另外两个表,如下面的代码:
const patientModel: IIncludeOptions = {
model: Patient,
attributes: ["deviceId", "firstName", "lastName"],
};
const deviceModel: IIncludeOptions = {
model: Device,
required: true,
attributes: ["deviceId", "deviceSerialNumber"],
include: [patientModel],
};
const eventCodeModel: IIncludeOptions = {
model: EventCode,
required: true,
attributes: ["name"],
};
const opts: IFindOptions = {
...pageSizeLimit,
offset : offsetNum,
attributes: ["id", "deviceId", "eventId", "dispatchedAt", "message", "createdAt"],
include: [
deviceModel,
eventCodeModel,
],
order: sortBy,
};
const resCount = await DeviceEvent.findAndCountAll(opts).then((response) => {
const totalItems = response.count;
return {
totalItems,
currentPage: page || 1,
};
});
最佳答案
尝试检查您的 SQL 记录查询。
按照这些说明获取查询转储:How can I see the SQL generated by Sequelize.js?
并查看计数数字是否与 findAndCountAll()
返回给您的相同。
也许 sequelize.js
挂载查询的方式会影响结果。
关于sequelize.js - findAndCountAll 计数得到一个比实际返回数据更大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64354987/