express-graphql - GraphQL 查询返回 null
问题描述
我运行了这个成功的突变,它被保存到我的 MongoDB 数据库中:
mutation {
addRecipe(name: "Chole Dhania Masala", category: "Indian", description: "A tasty dish", instructions: "Cook it") {
name
category
description
instructions
}
}
但是,当我对其运行查询时:
query {
getAllRecipes {
_id
name
category
likes
createdDate
}
}
我不清楚为什么我得到:
{
"data": {
"getAllRecipes": null
}
}
这是我的resolvers.js
文件:
exports.resolvers = {
Query: {
getAllRecipes: async (root, args, { Recipe }) => {
const allRecipes = await Recipe.find();
return allRecipes;
}
},
Mutation: {
addRecipe: async (root, { name, description, category, instructions, username }, { Recipe }) => {
const newRecipe = await new Recipe({
name,
description,
category,
instructions,
username
}).save();
return newRecipe;
}
}
};
解决方案
你有recipes
吗?
Query: {
getAllRecipes: async (root, args, { Recipe }) => {
const allRecipes = await Recipe.find();
if (allRecipes) {
return allRecipes;
}
return null;
}
}
推荐阅读
- reactjs - 需要使用带有 Axios 和 React.js 的 TicketMaster API 获取事件
- c - 键盘记录器在 VM 上工作,但不在我的真实计算机上
- python - 匹配后从数据框中复制列
- angular - 带有选项卡的 Angular 6 RouterLink
- json - 将三维 JSON 列表渲染为 FLASK 中的 HTML 模板
- python - Python/Pandas:识别系列中的值计数并报告。还报告“nan”计数
- c# - LINQ to CosmosDb Sql API 的逻辑与
- android - 服务中 CountDownTimer 的 onTick 方法未执行
- javascript - 如何将字符串转换为整数
- php - 从具有多个 Id 的同一 Mysql 列中获取数据