feathers-mongodb Service.find({query: {_id}}) returns 空
feathers-mongodb Service.find({query: {_id}}) returns null
我有以下架构:
students.graphql.schema.js
export default [
`
type StudentsWithPagination {
total: Int
items: [Students]
}
type Students {
_id: String!
name: String
address: Addresses
}
`,
];
addresses.graphql.schema.js
export default [
`
type AddressesWithPagination {
total: Int
items: [Addresses]
}
type Addresses {
_id: String!
title: String
}
`,
];
我已经通过 运行 feathers generate service
students.service.js 和 addresses.services.js[=52 创建了两个服务=].
当我通过 title
搜索地址时,我得到了结果。但是,当我按 _id
搜索时,我得到空值。类似于:
const studentsResolvers = {
Students: {
address: student => {
const query = {
_id: student.address
}
return Addresses.find({ query }).then(result => {
console.log(result)
})
}
}
}
上面的代码产生了 null
而 student.address
return 是正确的 address._id
。我仍然得到 null
即使我用正确的 address._id
硬编码 student.address
除非我按地址标题搜索,否则上面的代码将 return null
。类似于:
const query = {
title: 'my-location'
}
_id
是 String
类型,而不是 ObjectID
。
我做错了什么?
作为 documented in the feathers-mongodb adapter, since MongoDB itself (unlike Mongoose) does not have a schema, all query parameters have to be converted to the type in the database in a hook 手动。该示例可以相应地适应 $in
查询:
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);
}
}
});
我有以下架构:
students.graphql.schema.js
export default [
`
type StudentsWithPagination {
total: Int
items: [Students]
}
type Students {
_id: String!
name: String
address: Addresses
}
`,
];
addresses.graphql.schema.js
export default [
`
type AddressesWithPagination {
total: Int
items: [Addresses]
}
type Addresses {
_id: String!
title: String
}
`,
];
我已经通过 运行 feathers generate service
students.service.js 和 addresses.services.js[=52 创建了两个服务=].
当我通过 title
搜索地址时,我得到了结果。但是,当我按 _id
搜索时,我得到空值。类似于:
const studentsResolvers = {
Students: {
address: student => {
const query = {
_id: student.address
}
return Addresses.find({ query }).then(result => {
console.log(result)
})
}
}
}
上面的代码产生了 null
而 student.address
return 是正确的 address._id
。我仍然得到 null
即使我用正确的 address._id
student.address
除非我按地址标题搜索,否则上面的代码将 return null
。类似于:
const query = {
title: 'my-location'
}
_id
是 String
类型,而不是 ObjectID
。
我做错了什么?
作为 documented in the feathers-mongodb adapter, since MongoDB itself (unlike Mongoose) does not have a schema, all query parameters have to be converted to the type in the database in a hook 手动。该示例可以相应地适应 $in
查询:
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);
}
}
});