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
}
]
}
]
}
答案 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