有没有办法摆脱 GraphQL 中的 [Object: null prototype]

FIr*_*man 7 javascript mongoose node.js graphql apollo-server

我试图与一个一对多的关系数据库MongooseGraphQL

每当我将数据插入 GraphQL 突变参数时,我都会收到[Object: null prototype]错误消息。

我注意到[Object: null prototype]当我尝试console.log进行调试时,对象会在它前面。

我尝试了很多方法,尝试使用map()args 甚至使用replace()但没有运气。我得到的只是"args.ingredient.map/replace is not a function"

我通过更改参数来测试硬编码方法,例如:

args.category = '5c28c79af62fad2514ccc788'
args.ingredient = '5c28c8deb99a9d263462a086'
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,它适用于这种方法。我假设输入不能是一个对象而只是一个 ID。

实际结果见下文。

解析器

Query: {
    recipes: async (root, args, { req }, info) => {
        return Recipe.find({}).populate('ingredient category', 'name createdAt').exec().then(docs => docs.map(x => x))
    },
},
Mutation: {
    addRecipe: async (root, args, { req }, info) => {
      // args.category = '5c28c79af62fad2514ccc788'
      // args.ingredient = '5c28c8deb99a9d263462a086'
      // console.log(args.map(x => x))
      return Recipe.create(args)
    }
}
Run Code Online (Sandbox Code Playgroud)

类型定义

extend type Mutation {
    addRecipe(name: String!, direction: [String!]!, ingredient: [IngredientInput], category: [CategoryInput]): Recipe
}

type Recipe {
    id: ID!
    name: String!
    direction: [String!]!
    ingredient: [Ingredient!]!
    category: [Category!]!
}

input IngredientInput {
    id: ID!
}

input CategoryInput {
    id: ID!
}
Run Code Online (Sandbox Code Playgroud)

楷模

const recipeSchema = new mongoose.Schema({
    name: String,
    direction: [String],
    ingredient: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Ingredient' }],
    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' }
}, {
    timestamps: true // createdAt, updateAt
})

const Recipe = mongoose.model('Recipe', recipeSchema)
Run Code Online (Sandbox Code Playgroud)

这是我在插入数据时控制台记录参数的结果

{ 
    name: 'Butter Milk Chicken TEST2',
    direction: [ 'Step1', 'Step2', 'Step3' ],
    ingredient:[[Object: null prototype] { id: '5c28c8d6b99a9d263462a085' }],
    category: [[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }]
}
Run Code Online (Sandbox Code Playgroud)

我想我需要得到这样的东西

{ 
    name: 'Butter Milk Chicken TEST2',
    direction: [ 'Step1', 'Step2', 'Step3' ],
    args.category = ['5c28c79af62fad2514ccc788']
    args.ingredient = ['5c28c8ccb99a9d263462a083', '5c28c8d3b99a9d263462a084', '5c28c8d6b99a9d263462a085']
}
Run Code Online (Sandbox Code Playgroud)

小智 17

您可以执行以下操作,[对象:空原型] 将消失

const a = JSON.parse(JSON.stringify(args));
Run Code Online (Sandbox Code Playgroud)

args.category

[[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }], 
Run Code Online (Sandbox Code Playgroud)

JSON.parse(JSON.stringify(args.category) 将是 { id: '5c28c79af62fad2514ccc788' }


Jos*_*yck -1

我们遇到了这个问题。我们希望查询数据库中具有价格的服务对象。

\n\n

预期结果:

\n\n
service: {\n  price: 9999\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然而,我们不小心查询了 \xe2\x80\x9cservices\xe2\x80\x9d (而不是 \xe2\x80\x9cservice\xe2\x80\x9d),它给了我们一系列价格(只有一个价格),如下所示:

\n\n

[ [Object: null prototype] { price: 9.99 } ]

\n\n

这是由错误查询引起的。

\n\n

一旦我们将查询更改为 \xe2\x80\x9cservice\xe2\x80\x9d (而不是 \xe2\x80\x9cservices\xe2\x80\x9d),数据就会按预期返回,而没有空原型。

\n\n

我们使用 Prisma 作为我们的 ORM,但也许您在应该查询菜谱的时候正在查询菜谱。

\n