Mongodb find({query:{{u id}})返回null
我有以下模式: students.graphql.schema.jsMongodb find({query:{{u id}})返回null,mongodb,graphql,feathersjs,Mongodb,Graphql,Feathersjs,我有以下模式: students.graphql.schema.js export default [ ` type StudentsWithPagination { total: Int items: [Students] } type Students { _id: String! name: String address: Addresses } `, ]; export default [ ` type Ad
export default [
`
type StudentsWithPagination {
total: Int
items: [Students]
}
type Students {
_id: String!
name: String
address: Addresses
}
`,
];
export default [
`
type AddressesWithPagination {
total: Int
items: [Addresses]
}
type Addresses {
_id: String!
title: String
}
`,
];
addresses.graphql.schema.js
export default [
`
type StudentsWithPagination {
total: Int
items: [Students]
}
type Students {
_id: String!
name: String
address: Addresses
}
`,
];
export default [
`
type AddressesWithPagination {
total: Int
items: [Addresses]
}
type Addresses {
_id: String!
title: String
}
`,
];
我通过运行feathersgenerateservice
students.service.js和addresses.services.js创建了两个服务
当我通过标题搜索地址时,我得到了结果。但是,当我通过\u id
进行搜索时,我得到空值。比如:
const studentsResolvers = {
Students: {
address: student => {
const query = {
_id: student.address
}
return Addresses.find({ query }).then(result => {
console.log(result)
})
}
}
}
const query = {
title: 'my-location'
}
上面的代码生成null
,尽管student.address
返回正确的地址。\u id
。即使我使用正确的地址硬编码student.address
,我仍然会得到null
。\u id
除非我按地址标题搜索,否则上面的代码将返回null
。比如:
const studentsResolvers = {
Students: {
address: student => {
const query = {
_id: student.address
}
return Addresses.find({ query }).then(result => {
console.log(result)
})
}
}
}
const query = {
title: 'my-location'
}
\u id
的类型为String
,而不是ObjectID
我做错了什么?因为MongoDB本身(与Mongoose不同)没有模式,所以必须手动将所有查询参数转换为数据库中的类型。该示例可相应地适用于
查询中的$:
const ObjectID=require('mongodb').ObjectID
app.service('users').hooks({
before: {
find(context) {
const { query = {} } = context.params;
if(query._id) {
query._id = new ObjectID(query._id);
}
if(query.age !== undefined) {
query.age = parseInt(query.age, 10);
}
context.params.query = query;
return Promise.resolve(context);
}
}
});
_id作为objectId或string存储在DB中?\ id作为string not objectId存储在DB中使用findById
时的结果是什么?我得到findById
不是一个函数