最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to get rid of [Object: null prototype] in GraphQL - Stack Overflow

programmeradmin3浏览0评论

I'm trying to make one-to-many relationship database with Mongoose and GraphQL.

Whenever I insert the data to GraphQL mutation argument, I will get [Object: null prototype] error.

I notice the object will have [Object: null prototype] in front of it when I tried to console.log for debug purpose.

I have tried many ways, tried to map() args or even to use replace() but no luck. All I have been getting is "args.ingredient.map/replace is not a function"

I have test hard coded method by changing the args for example:

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

Surprisingly it works with this method. I assume the input cannot be an object but just an ID.

Refer below for actual results.

Resolvers

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

TypeDef

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

Models

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)

This is the result I console log the args when inserting the data

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

I assume I need to get something like this

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

I'm trying to make one-to-many relationship database with Mongoose and GraphQL.

Whenever I insert the data to GraphQL mutation argument, I will get [Object: null prototype] error.

I notice the object will have [Object: null prototype] in front of it when I tried to console.log for debug purpose.

I have tried many ways, tried to map() args or even to use replace() but no luck. All I have been getting is "args.ingredient.map/replace is not a function"

I have test hard coded method by changing the args for example:

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

Surprisingly it works with this method. I assume the input cannot be an object but just an ID.

Refer below for actual results.

Resolvers

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

TypeDef

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

Models

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)

This is the result I console log the args when inserting the data

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

I assume I need to get something like this

{ 
    name: 'Butter Milk Chicken TEST2',
    direction: [ 'Step1', 'Step2', 'Step3' ],
    args.category = ['5c28c79af62fad2514ccc788']
    args.ingredient = ['5c28c8ccb99a9d263462a083', '5c28c8d3b99a9d263462a084', '5c28c8d6b99a9d263462a085']
}
Share Improve this question edited Mar 22, 2020 at 15:07 matthias_h 11.4k9 gold badges23 silver badges40 bronze badges asked Dec 31, 2018 at 3:30 FIrmanFIrman 1311 gold badge2 silver badges8 bronze badges 4
  • Where would the arrays come from? – Bergi Commented Feb 17, 2019 at 13:31
  • Where exactly do you "get [Object: null prototype] error", in the Recipe.create line? Can you post the full error message, please – Bergi Commented Feb 17, 2019 at 13:32
  • I filed as an issue in the apollo project: github.com/apollographql/apollo-server/issues/3149 – Public Profile Commented Aug 12, 2019 at 3:45
  • This has been very quickly fixed in firestore. github.com/googleapis/nodejs-firestore/pull/736 – Public Profile Commented Aug 23, 2019 at 3:03
Add a comment  | 

4 Answers 4

Reset to default 20

You can do something like below,and [Object: null prototype] would disappear

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

args.category is

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

JSON.parse(JSON.stringify(args.category) would be { id: '5c28c79af62fad2514ccc788' }

Normally, when passing InputTypes as an argument, I solve it using destructuring, like this:

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

Try destructuring assignment with the args parameter. Your problem happens because args is an object that holds the mutation arguments. After destructuring it, you're gonna be able to access your argument directly:

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

We had this problem. We were looking to query a service object in the database that had a price on it.

Expected Result:

service: {
  price: 9999
}

However, we accidentally queried “services” (instead of “service”) which gave us an array of prices (with only one price) like so:

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

This was caused by a bad query.

Once we changed the query to “service” (instead of “services”) the data came back as expected without the null prototype.

We use Prisma as our ORM though but perhaps you are querying for recipes when you should be querying for recipe.

发布评论

评论列表(0)

  1. 暂无评论