gpt4 book ai didi

sequelize.js - GraphQL 错误 : Expected Iterable while using Sequelize findAndCountAll function

转载 作者:行者123 更新时间:2023-12-03 22:20:10 35 4
gpt4 key购买 nike

我的问题

你好!
我正在尝试使用 Meteor、Apollo、React 和 Sequelize 实现一个简单的分页。我使用 findAll() 函数查询我想要的内容没有问题,但由于我还想要受查询影响的总行数,因此我使用 findAndCountAll() 但我收到此错误:

Unhandled (in react-apollo) Error: GraphQL error: Expected Iterable, but did not find one for field Query.books



解析器.js
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 },
});

我的 graphQL 查询
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
}
}
`;

我觉得有些东西不见了,但我找不到它是什么。如果您需要其他任何东西,请告诉我。

最佳答案

findAndCountAll 函数作为结果返回一个具有 2 个属性 {count: rowsCount, rows: arrayOfModelInstances} 的对象。你应该迭代 rows

关于sequelize.js - GraphQL 错误 : Expected Iterable while using Sequelize findAndCountAll function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44269751/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com