Trying to delete using Mutation but its giving the error saying authorsCollection.delete is not a function

时间:2016-08-31 12:33:14

标签: graphql relay graphql-js

When I try to run it, It is giving me the error.Trying to delete using Mutation but its giving the error saying "authorsCollection.delete" is not a function

const Mutation = new GraphQLObjectType({
  name: "Mutations",
  fields: {
    DeleteAuthor: {
      type: Author,
      args: {
        _id: {type: new GraphQLNonNull(GraphQLString)},
        name: {type: GraphQLString},

      },
      resolve: function(rootValue, args) {
        let author = Object.assign({}, args);
        console.log(args);
        return authorsCollection.delete(author._id)
          .then(_ => author);
      }
    }

What should be edited in the code so that I can implemented the Delete operation?

It is giving me the error as below

{
  "data": {
    "DeleteAuthor": null
  },
  "errors": [
    {
      "message": "authorsCollection.delete is not a function",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

错误消息authorsCollection.delete is not a function表示authorsCollection没有任何名为delete的函数。在你的jsfiddle代码中,我看到你使用了promised-mongo库。删除文档的API为remove,而不是delete

更改为

authorsCollection.remove({_id: ObjectId(author._id)})

传递的_id是一个字符串。但DB中作者的_id属性类型为ObjectID。因此,您必须将其转换为ObjectID类型。您可以使用promised-mongo的{​​{1}}来完成此操作。所以,在开头导入它:

ObjectId