Mongodb 在查询特定id时,graphql中出现错误
模式: 在这里,我通过id从mongodb获取数据 我正在为mongodb客户端使用mongoose。 我用express和graphql安装了服务器 我在下面进行查询,以通过指定id获得结果Mongodb 在查询特定id时,graphql中出现错误,mongodb,graphql,Mongodb,Graphql,模式: 在这里,我通过id从mongodb获取数据 我正在为mongodb客户端使用mongoose。 我用express和graphql安装了服务器 我在下面进行查询,以通过指定id获得结果 const UserType = new GraphQLObjectType({ name: 'User', fields: () => ({ id : { type: GraphQLID }, name : { type: GraphQLString
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id : { type: GraphQLID },
name : { type: GraphQLString },
email : { type: GraphQLString },
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {
id: { type: GraphQLID }
},
resolve(parent, args){
return User.findById(args.id);
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery
});
但是,我在进行查询时遇到了以下错误
{
user(id: 2){
name
}
}
我在数据库中的id只是一个整数值,如下所示
{
"errors": [
{
"message": "Cast to ObjectId failed for value \"2\" at path \"_id\" for model \"User\"",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"user"
]
}
],
"data": {
"user": null
}
}
请看一看。
类型:UserType
这应该是类型:新的graphqlist(UserType),
{
"_id" : ObjectId("5d49c6dc54f1cec013ee6b49"),
"id" : 10,
"name" : "Clementina DuBuque",
"email" : "Rey.Padberg@karina.biz",
}