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 id为空的graphql查询返回对象_Mongodb_Graphql_Aggregate - Fatal编程技术网

Mongodb id为空的graphql查询返回对象

Mongodb id为空的graphql查询返回对象,mongodb,graphql,aggregate,Mongodb,Graphql,Aggregate,Graphql返回id为空的项目。 与mongodb合作 我觉得很奇怪 如果我在MailType id上删除新的GraphQLNonNull(), 它与id:null一起工作,另一个字段工作正常 const MailType = new GraphQLObjectType({ name: 'Mail', fields: () => ({ id: { type: new GraphQLNonNull(GraphQLID), }, ... }) const Q

Graphql返回id为空的项目。 与mongodb合作

我觉得很奇怪

如果我在MailType id上删除新的GraphQLNonNull(), 它与
id:null
一起工作,另一个字段工作正常

const MailType = new GraphQLObjectType({
    name: 'Mail',
    fields: () => ({
        id: { type: new GraphQLNonNull(GraphQLID), },
...
})

const Query = {
    mails: {
        type: new GraphQLList(MailType),
        args: {
            senderId: { type: GraphQLID },
            isOffline: { type: GraphQLBoolean },
        },
        async resolve(root, args, req, ctx) {
            if (args.isOffline === false) {
                let a = await model.aggregate([
                  { $match: { isOffline: false } },
                ]);
                let b = await model.find({ isOffline: false });

                console.log(JSON.stringify(a) == JSON.Stringify(b)) /// return true
                return a // error
                return b // working
            }
            return model.find({senderId: args.senderId});
        }
    }
}
我有两个小时的麻烦,但我没有得到答案。
有人能帮我吗?

您可能在mongodb模式中出错,而不是在graphQl中。 确保您没有通过
id
键定义您的id,它应该是
\u id

例如,如果您正在使用猫鼬,它可以是这样的:

const MailSchema = new Schema({
  _id: {
    type: String,
    unique: true,
  },
  ....
  ....
});

它只是意味着当您返回
a
时,id字段为null,这不应该是因为您已将id定义为
GraphQLNonNull
,因此出现错误。@Boney a,b是邮件数组。两者都有有效id的邮件。
const MailSchema = new Schema({
  _id: {
    type: String,
    unique: true,
  },
  ....
  ....
});