Node.js – GraphQL Expected Iterable, but did not find one for field while using find

apollographqlnode.jssequelize.js

Currently I am using Apollo/GraphQL/Node.js/Sequelize to build my backend server, and my server code looked like below, in there I can use req.user to get the current login user

app.use(
  '/graphql',
  bodyParser.json(),
  graphqlExpress(req => ({
    schema,
    context: {
      models,
      user: req.user,
    },
  })),
);

Now I have two models User and Recipe, and the association rule is Recipe belongs to User, so in the Recipe schema I can use the UserId to know which user create this schema, the Recipe schema is

type Recipe {
  id: Int!
  authorName: String!
  authorFbPage: String @virtual
  perfumeName: String!
  message: String
  UserId: Int
}

type Query {
  allRecipe: [Recipe]
  meRecipe: [Recipe]
  AvailableWatchRecipe: [Recipe]
}

My problem is in the meRecipe part, this Query supposed to be able to show the recipes created by login user, the resolver code is

meRecipe: async (parent, args, { models, user }) => {
      if (user) {
        console.log(user.id);
        console.log(user.username);
        return models.Recipe.find({ where: { UserId: user.id } })
          .then((result) => { return result });
      }

      return null;
    }, 

You can see I also use the console.log to check whether I can get the current user information, it actually can, so I am really confused why when I run this Query in the GraphQL server, it always shows "message": "Expected Iterable, but did not find one for field Query.meRecipe.
I have checked these resources:
https://github.com/brysgo/graphql-bookshelf/issues/10
and
GraphQL Expected Iterable, but did not find one for field xxx.yyy
but none of them fit my case, can anyone give me some advice, thanks!

Best Answer

Instead of using :

models.Recipe.find

Use

models.Recipe.findAll // this will return single result in array
Related Topic