尝试使用 Mutation 删除,但它给出错误提示 authorsCollection.delete is not a function
Trying to delete using Mutation but its giving the error saying authorsCollection.delete is not a function
当我尝试 运行 它时,它给我 error.Trying 使用 Mutation 删除但它给出错误说 "authorsCollection.delete" 不是函数
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);
}
}
我应该在代码中编辑什么才能实现删除操作?
它给我如下错误
{
"data": {
"DeleteAuthor": null
},
"errors": [
{
"message": "authorsCollection.delete is not a function",
"locations": [
{
"line": 2,
"column": 3
}
]
}
]
}
错误消息 authorsCollection.delete is not a function
表明 authorsCollection
没有任何名为 delete
的函数。在您的 jsfiddle 代码中,我看到您使用了 promised-mongo 库。删除文档的 API 是 remove
,而不是 delete
。
改为
authorsCollection.remove({_id: ObjectId(author._id)})
传递的_id
是一个字符串。但是DB中author的_id
属性是ObjectID
类型。因此,您必须将其转换为 ObjectID
类型。您可以使用 promised-mongo
的 ObjectId
来实现。所以,在开头导入它:
import {ObjectId} from 'promised-mongo';
当我尝试 运行 它时,它给我 error.Trying 使用 Mutation 删除但它给出错误说 "authorsCollection.delete" 不是函数
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);
}
}
我应该在代码中编辑什么才能实现删除操作?
它给我如下错误
{
"data": {
"DeleteAuthor": null
},
"errors": [
{
"message": "authorsCollection.delete is not a function",
"locations": [
{
"line": 2,
"column": 3
}
]
}
]
}
错误消息 authorsCollection.delete is not a function
表明 authorsCollection
没有任何名为 delete
的函数。在您的 jsfiddle 代码中,我看到您使用了 promised-mongo 库。删除文档的 API 是 remove
,而不是 delete
。
改为
authorsCollection.remove({_id: ObjectId(author._id)})
传递的_id
是一个字符串。但是DB中author的_id
属性是ObjectID
类型。因此,您必须将其转换为 ObjectID
类型。您可以使用 promised-mongo
的 ObjectId
来实现。所以,在开头导入它:
import {ObjectId} from 'promised-mongo';