Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb find({query:{{u id}})返回null_Mongodb_Graphql_Feathersjs - Fatal编程技术网

Mongodb find({query:{{u id}})返回null

Mongodb 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

我有以下模式:

students.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
  }
`,
];
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.jsaddresses.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
不是一个函数