【问题标题】:GraphQL error: Expected Iterable while using Sequelize findAndCountAll functionGraphQL 错误:使用 Sequelize findAndCountAll 函数时预期可迭代
【发布时间】:2017-10-31 08:55:38
【问题描述】:

我的问题

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

未处理(在 react-apollo 中)错误:GraphQL 错误:预期可迭代,但没有为字段 Query.books 找到一个

resolvers.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
    }
  }
`;

我认为缺少某些东西,但我找不到它是什么。如果您还有其他需要,请告诉我。

【问题讨论】:

  • 感谢@Tilekbekov Yrysbek 通过将.then((result) => { return result.rows; }); 添加到我的findAndCountAll 函数中解决了这个问题

标签: sequelize.js react-apollo


【解决方案1】:

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

【讨论】:

  • 感谢您的回复!问题是我自己没有迭代结果。因为我使用的是 Meteor,所以它为我做:const schema = makeExecutableSchema({ typeDefs, resolvers }); 我只是将它传递给解析器。
猜你喜欢
  • 2021-11-13
  • 2022-01-27
  • 2021-04-09
  • 2021-09-14
  • 2019-09-25
  • 2023-01-23
  • 1970-01-01
  • 2020-06-01
  • 2012-12-03
相关资源
最近更新 更多