无法将数据插入具有关联的集合的数据库中。

huangapple go评论85阅读模式
英文:

Unable to insert data into db where a collection has a relationship

问题

这是在一个Node项目中,我正在尝试为一个简单的博客编写一个graphql API。

我正在使用apollo,并在后台尝试通过mongoose将数据保存到mongo数据库。

当我尝试保存帖子数据时,我目前正在收到以下错误。

"GraphQLError: ID cannot represent value: { type: \"Buffer\", data: [Array] }",
"at GraphQLScalarType.serialize (/Users/name/projects/graphtime/node_modules/graphql/type/scalars.js:301:11)",

我认为它在Post的作者插入部分出现问题。

我的模式或我在Apollo GraphiEditor上执行查询的方式是否有问题?

这是数据库模式

import mongoose from "mongoose";

const authorSchema = new mongoose.Schema({
  name: { type: String, required: true },
  avatar: { type: String },
});

export const Author = mongoose.model('Author', authorSchema);

const postSchema = new mongoose.Schema({
  title: { type: String, required: true },
  authors: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Author'
    }
  ]
});

export const Post = mongoose.model('Post', postSchema);

这是GraphQL模式

type Post {
    id: ID!
    title: String
    authors: [Author]
}

type Author {
    id: ID!
    name: String
    avatar: String
}

type Mutation {
    addPost(
        title: String!,
        authors: [ID!],
    ): Post
}

这是GraphQL解析器

const resolvers = {
  Mutation: {
    addPost: (_, args, context) => {
      const newPost = new Post({
        title: args.title,
        authors: args.authors,
      });
      return newPost.save();
    },
  },
};

这是我在编辑器中查询时引发错误的方式。

mutation {
  addPost(
    title: "New Post",
    authors: ["63babc44e18d174016b03433"],
  ) {
    title
    authors {
      id
      name
    }
  }
}

希望这些信息有助于解决您遇到的问题。

英文:

This is in a Node project where I am trying to write a graphql api for a simple blog.
<br/>I am using apollo and behind the scenes attempting to save data to a mongo db via mongoose.

I am currently getting the following error when I try to save a Post data.

> "GraphQLError: ID cannot represent value: { type: &quot;Buffer&quot;, data:
> [Array] }", "at GraphQLScalarType.serialize
> (/Users/name/projects/graphtime/node_modules/graphql/type/scalars.js:301:11)",

It is complaining about the Author insertion as part of the Post I believe.

Is there something wrong with my schema or how I am performing the query on the Apollo GraphiEditor?

The is the DB Schema

import mongoose from &quot;mongoose&quot;;

const authorSchema = new mongoose.Schema({
  name: { type: String, required: true },
  avatar: { type: String },
});

export const Author = mongoose.model(&#39;Author&#39;, authorSchema);

const postSchema = new mongoose.Schema({
  title: { type: String, required: true },
  authors: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: &#39;Author&#39;
    }
  ]
});

export const Post = mongoose.model(&#39;Post&#39;, postSchema);

This is the graphql Schema

type Post {
    id: ID!
    title: String
    authors: [Author]
}

type Author {
    id: ID!
    name: String
    avatar: String
}

type Mutation {
    addPost(
        title: String!,
        authors: [ID!],
    ): Post
}

This is the grapql resolver.

const resolvers = {
  Mutation: {
    addPost: (_, args, context) =&gt; {
      const newPost = new Post({
        title: args.title,
        authors: args.authors,
      });
      return newPost.save();
    },
  },
};

This is how I am querying in the editor which throws the error.
<br/>P.S: The DB already has an existing author in there and lets say the id for that row is: 63babc44e18d174016b03433

mutation {
  addPost(
    title: &quot;New Post&quot;,
    authors: [&quot;63babc44e18d174016b03433&quot;],
  ) {
    title
    authors {
      id
      name
    }
  }
}

答案1

得分: 1

你已经以这种方式定义了mutation:

type Mutation {
    addPost(
        title: String!,
        authors: [ID!],
    ): Post
}

这意味着addPost会返回一个Post对象,它的结构如下:

type Post {
    id: ID!
    title: String
    authors: [Author]
}

因此,authors是一个包含Author对象的数组,而不是_id的数组。

虽然你将ID保存到了数据库中,但返回的对象与[Author]不匹配。还要注意,postSchemamongoose.Schema.Types.ObjectId。虽然它是对作者的引用,但它是一个包含ID的数组。

因此,你可以返回一个ID数组而不是Author对象,或者在mongoose中填充查询,因为你有引用关系。

英文:

You have defined the mutation in this way:

type Mutation {
    addPost(
        title: String!,
        authors: [ID!],
    ): Post
}

That's mean addPost return a Post object, which is:

type Post {
    id: ID!
    title: String
    authors: [Author]
}

So authors is an array of Authors objects, not the _ids.

You are getting the ID and saving into DB but the returned object does not match [Author]. Also note how the postSchema is mongoose.Schema.Types.ObjectId. Referenced to Authors, yes, but is an array of IDs.

So you can return an array of IDs instead the Author object or populate the query in mongoose because you have the reference.

huangapple
  • 本文由 发表于 2023年1月9日 06:06:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定