嗨! 我尝试使用Meteor,Apollo,React和Sequelize实现简单的分页。我使用 findAll()函数查询我想要的东西没有问题,但由于我还想要查询影响的总行数,我使用的是 findAndCountAll()但是我得到这个错误:
未处理(在react-apollo中)错误:GraphQL错误:预期为Iterable,但没有为字段Query.books找到一个
export const resolvers = {
Query: {
async books(root, args, context) {
return Book.findAndCountAll({
where: {},
offset: args.offset,
limit: args.limit,
});
}
}
};
export const typeDefs =`
type Book {
id: String!
title: String!
author: String!
price: Float!
quantity: Float!
isbn: String!
publisher: String!
description: String
collectionName: String
collectionNumber: Int
serieName: String
serieNumber: Int
}
type Query {
books(limit: Int, offset: Int): [Book]
}
`;
const BookModel = db.define('book', {
title: { type: Sequelize.STRING },
author: { type: Sequelize.STRING },
price: { type: Sequelize.FLOAT },
quantity: { type: Sequelize.FLOAT },
isbn: { type: Sequelize.STRING },
publisher: { type: Sequelize.STRING },
description: { type: Sequelize.STRING },
collectionName: { type: Sequelize.STRING },
collectionNumber: { type: Sequelize.INTEGER },
serieName: { type: Sequelize.STRING },
serieNumber: { type: Sequelize.INTEGER },
});
const allBooks = gql`
query BooksForDisplay($limit: Int, $offset: Int) {
books(limit: $limit, offset: $offset) {
id,
title,
author,
price,
quantity,
isbn,
publisher,
description,
collectionName,
collectionNumber,
serieName,
serieNumber
}
}
`;
我认为缺少了一些东西,但我找不到它是什么。如果您还有其他需要,请告诉我。
答案 0 :(得分:0)
findAndCountAll
函数返回一个具有2个属性{count: rowsCount, rows: arrayOfModelInstances}
的对象。你应该迭代rows