【问题标题】:Is there a way to get rid of [Object: null prototype] in GraphQL有没有办法摆脱 GraphQL 中的 [Object: null prototype]
【发布时间】:2019-05-27 17:48:34
【问题描述】:

我正在尝试使用MongooseGraphQL 建立一对多关系数据库。

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

当我尝试使用console.log 进行调试时,我注意到该对象前面会有[Object: null prototype]

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

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

args.category = '5c28c79af62fad2514ccc788'
args.ingredient = '5c28c8deb99a9d263462a086'

令人惊讶的是,它适用于这种方法。我假设输入不能是一个对象,而只是一个 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)
    }
}

类型定义

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!
}

型号

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)

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

{ 
    name: 'Butter Milk Chicken TEST2',
    direction: [ 'Step1', 'Step2', 'Step3' ],
    ingredient:[[Object: null prototype] { id: '5c28c8d6b99a9d263462a085' }],
    category: [[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }]
}

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

{ 
    name: 'Butter Milk Chicken TEST2',
    direction: [ 'Step1', 'Step2', 'Step3' ],
    args.category = ['5c28c79af62fad2514ccc788']
    args.ingredient = ['5c28c8ccb99a9d263462a083', '5c28c8d3b99a9d263462a084', '5c28c8d6b99a9d263462a085']
}

【问题讨论】:

标签: javascript node.js mongoose graphql apollo-server


【解决方案1】:

通常,当将 InputTypes 作为参数传递时,我使用解构来解决它,如下所示:

addRecipe: async (root, { ...args }, { req }, info) => {
   // args.category = '5c28c79af62fad2514ccc788'
   // args.ingredient = '5c28c8deb99a9d263462a086'
   // console.log(args.map(x => x))
   return Recipe.create(args)
}

【讨论】:

    【解决方案2】:

    尝试 destructuring assignmentargs 参数。发生您的问题是因为 args 是一个包含突变参数的对象。解构它之后,您将能够直接访问您的论点:

    Mutation: {
        addRecipe: async (root, { args }, { req }, info) => {
          return Recipe.create(args)
        }
    }
    

    【讨论】:

      【解决方案3】:

      在操场上,我将设置请求凭据从省略更改为包含,它起作用了,"request.credentials": "include" 我希望它有所帮助。

      【讨论】:

        【解决方案4】:

        你可以像下面这样做,[Object: null prototype]会消失

        const a = JSON.parse(JSON.stringify(args));
        

        args.category

        [[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }], 
        

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

        【讨论】:

        • 请为您的回答提供一些解释。
        • @amanb 之所以可行,是因为您首先使用 JSON.strigify(args) 将 args 的值从 [[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }] 转换为 [{ id: ' 5c28c79af62fad2514ccc788' }] 然后使用 JSON.parse 将其转换为有效的 JS 值。就我而言,我更喜欢使用JSON.strigify(args[0]) 来实现{ name: 'test', type: 'A' } 所需的结果
        【解决方案5】:

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

        预期结果:

        service: {
          price: 9999
        }
        

        但是,我们不小心查询了“服务”(而不是“服务”),这给了我们一系列价格(只有一个价格),如下所示:

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

        这是由错误的查询引起的。

        一旦我们将查询更改为“服务”(而不是“服务”),数据就会按预期返回而没有空原型。

        虽然我们使用 Prisma 作为我们的 ORM,但是当您应该查询食谱时,也许您正在查询食谱。

        【讨论】:

        • 当我运行创建突变并返回结果时,我得到了空原型,所以不确定它是否是一个命名的东西。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 1970-01-01
        • 2016-03-16
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多