无法读取null的属性(读取' _doc')

发布于 2025-02-11 10:50:24 字数 1466 浏览 1 评论 0原文

我正在在GraphQL中运行查询并获取此错误:

{
  "errors": [
    {
      "message": "Cannot read properties of null (reading '_doc')",
      "locations": [
        {
          "line": 38,
          "column": 5
        }
      ],
      "path": [
        "songs",
        0,
        "creator"
      ]
    }
  ],
  "data": null
}

这是查询。

query {
  songs {
    song_file_name
    song_type
    song_size
    user_name
    creator {
      email
    }

这是我的分辨率文件,这是我的解决方案。

const { buildSchema } = require('graphql');

module.exports = buildSchema(`
type Song {
  _id: ID!
  song_file_name: String!
  song_type: String!
  song_size: Int!
  user_name: String!
  creator: User!
}
type User {
  _id: ID!
  email: String!
  password: String
  createdSongs: [Song!]
}
input SongInput {
  song_file_name: String!
  song_type: String!
  song_size: Int!
  user_name: String!
}
input UserInput {
  email: String!
  password: String!
}
type RootQuery {
    songs: [Song!]!
}
type RootMutation {
    createSong(songInput: SongInput): Song
    createUser(userInput: UserInput): User
}
schema {
    query: RootQuery
    mutation: RootMutation
}
`);

谁能帮我弄清楚我做错了什么?看来创建者正在返回零,但据我所知,它是正确定义的。是什么原因造成的?

另外,谁能建议学习GraphQL的好地方?我正在关注

我知道GraphQl的基础知识,但是关系概念对我来说很难缠绕。

I'm running a query in GraphQL and getting this error:

{
  "errors": [
    {
      "message": "Cannot read properties of null (reading '_doc')",
      "locations": [
        {
          "line": 38,
          "column": 5
        }
      ],
      "path": [
        "songs",
        0,
        "creator"
      ]
    }
  ],
  "data": null
}

This is the query.

query {
  songs {
    song_file_name
    song_type
    song_size
    user_name
    creator {
      email
    }

This is my resolver file, and this is the schema.

const { buildSchema } = require('graphql');

module.exports = buildSchema(`
type Song {
  _id: ID!
  song_file_name: String!
  song_type: String!
  song_size: Int!
  user_name: String!
  creator: User!
}
type User {
  _id: ID!
  email: String!
  password: String
  createdSongs: [Song!]
}
input SongInput {
  song_file_name: String!
  song_type: String!
  song_size: Int!
  user_name: String!
}
input UserInput {
  email: String!
  password: String!
}
type RootQuery {
    songs: [Song!]!
}
type RootMutation {
    createSong(songInput: SongInput): Song
    createUser(userInput: UserInput): User
}
schema {
    query: RootQuery
    mutation: RootMutation
}
`);

Could anyone help me figure out what I'm doing wrong? It looks like creator is returning null, but it is properly defined as far as I can tell. What's causing this?

Also, can anyone suggest a good place to learn GraphQL? I'm following this guide and though I find it useful and informative, it quickly gets to a level where I don't feel I fully understand what I'm doing although I'm following along.

I know the basics of GraphQL, but the relational concepts is hard for me to wrap my head around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文