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












1















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



























    1















    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

























      1












      1








      1


      1






      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














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






      javascript node.js graphql apollo-server






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 3:30









      FIrmanFIrman

      225




      225
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53983315%2fis-there-a-way-to-get-rid-of-object-null-prototype-in-graphql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53983315%2fis-there-a-way-to-get-rid-of-object-null-prototype-in-graphql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas