Skip to content
Advertisement

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

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']
}

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement