CastError: Cast to embedded failed for value












0















I'm using Nodejs Express and Mongoose ("mongoose": "^5.3.11",). I'm trying to add some data to a collection. I'm getting all the correct data but when I use $push there is an error "CastError: Cast to embedded failed for value ", I've been looking around for different solutions but I can't figure it out.



this is my Schema



var InstructorSchema= mongoose.Schema({
first_name:{
type: String
},
last_name:{
type:String
},
username:{
type:String
},
email:{
type:String
},
classes:[{
class_id:{type:[mongoose.Schema.Types.ObjectId]},
class_code:{type:String},
class_title:{type:String}
}]
});

var Instructor = module.exports = mongoose.model('Instructor', InstructorSchema);


Here I collect the data from a form



info=;
info['instructor_ID']=req.user.id;
info['instructor_username']=req.user.username;
info['class_id']=req.body.class_id;
info['class_code']=req.body.class_code;
info['class_title']=req.body.class_title;


This is my data



module.exports.register=function(info,callback){
instructor_ID=info['instructor_ID'];
instructor_username=info['instructor_username'];
class_id=info['class_id'];
class_code=info['class_code'];
class_title=info['class_title'];


Here is the function



var query = {username: instructor_username};
Instructor.findOneAndUpdate(
query,
{
$push: {
"classes": {
class_id: class_id,
class_code: class_code,
class_title: class_title
}
}
},
{ safe: true,
upsert: true
},
callback
);


This is the error



    CastError: Cast to embedded failed for value "{ class_id: '5be353ead45965c74d9e2853 ',n  class_code: 'ART101 ',n  class_title: 'Arts Fundamentals ' }" at path "classes"
at new CastError (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooseliberrorcast.js:29:11)
at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:349:19)
at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:276:17)
at castUpdateVal (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:365:24)
at walkUpdatePath (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:165:24)
at castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:79:18)
at model.Query._castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3861:10)
at castDoc (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3889:18)
at model.Query.Query._findAndModify (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3021:19)
at model.Query.Query._findOneAndUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:2732:8)
at C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:278:20
at _next (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:102:16)
at process.nextTick (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:499:38)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)









share|improve this question





























    0















    I'm using Nodejs Express and Mongoose ("mongoose": "^5.3.11",). I'm trying to add some data to a collection. I'm getting all the correct data but when I use $push there is an error "CastError: Cast to embedded failed for value ", I've been looking around for different solutions but I can't figure it out.



    this is my Schema



    var InstructorSchema= mongoose.Schema({
    first_name:{
    type: String
    },
    last_name:{
    type:String
    },
    username:{
    type:String
    },
    email:{
    type:String
    },
    classes:[{
    class_id:{type:[mongoose.Schema.Types.ObjectId]},
    class_code:{type:String},
    class_title:{type:String}
    }]
    });

    var Instructor = module.exports = mongoose.model('Instructor', InstructorSchema);


    Here I collect the data from a form



    info=;
    info['instructor_ID']=req.user.id;
    info['instructor_username']=req.user.username;
    info['class_id']=req.body.class_id;
    info['class_code']=req.body.class_code;
    info['class_title']=req.body.class_title;


    This is my data



    module.exports.register=function(info,callback){
    instructor_ID=info['instructor_ID'];
    instructor_username=info['instructor_username'];
    class_id=info['class_id'];
    class_code=info['class_code'];
    class_title=info['class_title'];


    Here is the function



    var query = {username: instructor_username};
    Instructor.findOneAndUpdate(
    query,
    {
    $push: {
    "classes": {
    class_id: class_id,
    class_code: class_code,
    class_title: class_title
    }
    }
    },
    { safe: true,
    upsert: true
    },
    callback
    );


    This is the error



        CastError: Cast to embedded failed for value "{ class_id: '5be353ead45965c74d9e2853 ',n  class_code: 'ART101 ',n  class_title: 'Arts Fundamentals ' }" at path "classes"
    at new CastError (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooseliberrorcast.js:29:11)
    at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:349:19)
    at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:276:17)
    at castUpdateVal (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:365:24)
    at walkUpdatePath (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:165:24)
    at castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:79:18)
    at model.Query._castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3861:10)
    at castDoc (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3889:18)
    at model.Query.Query._findAndModify (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3021:19)
    at model.Query.Query._findOneAndUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:2732:8)
    at C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:278:20
    at _next (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:102:16)
    at process.nextTick (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:499:38)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)









    share|improve this question



























      0












      0








      0








      I'm using Nodejs Express and Mongoose ("mongoose": "^5.3.11",). I'm trying to add some data to a collection. I'm getting all the correct data but when I use $push there is an error "CastError: Cast to embedded failed for value ", I've been looking around for different solutions but I can't figure it out.



      this is my Schema



      var InstructorSchema= mongoose.Schema({
      first_name:{
      type: String
      },
      last_name:{
      type:String
      },
      username:{
      type:String
      },
      email:{
      type:String
      },
      classes:[{
      class_id:{type:[mongoose.Schema.Types.ObjectId]},
      class_code:{type:String},
      class_title:{type:String}
      }]
      });

      var Instructor = module.exports = mongoose.model('Instructor', InstructorSchema);


      Here I collect the data from a form



      info=;
      info['instructor_ID']=req.user.id;
      info['instructor_username']=req.user.username;
      info['class_id']=req.body.class_id;
      info['class_code']=req.body.class_code;
      info['class_title']=req.body.class_title;


      This is my data



      module.exports.register=function(info,callback){
      instructor_ID=info['instructor_ID'];
      instructor_username=info['instructor_username'];
      class_id=info['class_id'];
      class_code=info['class_code'];
      class_title=info['class_title'];


      Here is the function



      var query = {username: instructor_username};
      Instructor.findOneAndUpdate(
      query,
      {
      $push: {
      "classes": {
      class_id: class_id,
      class_code: class_code,
      class_title: class_title
      }
      }
      },
      { safe: true,
      upsert: true
      },
      callback
      );


      This is the error



          CastError: Cast to embedded failed for value "{ class_id: '5be353ead45965c74d9e2853 ',n  class_code: 'ART101 ',n  class_title: 'Arts Fundamentals ' }" at path "classes"
      at new CastError (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooseliberrorcast.js:29:11)
      at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:349:19)
      at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:276:17)
      at castUpdateVal (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:365:24)
      at walkUpdatePath (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:165:24)
      at castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:79:18)
      at model.Query._castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3861:10)
      at castDoc (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3889:18)
      at model.Query.Query._findAndModify (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3021:19)
      at model.Query.Query._findOneAndUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:2732:8)
      at C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:278:20
      at _next (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:102:16)
      at process.nextTick (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:499:38)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)









      share|improve this question
















      I'm using Nodejs Express and Mongoose ("mongoose": "^5.3.11",). I'm trying to add some data to a collection. I'm getting all the correct data but when I use $push there is an error "CastError: Cast to embedded failed for value ", I've been looking around for different solutions but I can't figure it out.



      this is my Schema



      var InstructorSchema= mongoose.Schema({
      first_name:{
      type: String
      },
      last_name:{
      type:String
      },
      username:{
      type:String
      },
      email:{
      type:String
      },
      classes:[{
      class_id:{type:[mongoose.Schema.Types.ObjectId]},
      class_code:{type:String},
      class_title:{type:String}
      }]
      });

      var Instructor = module.exports = mongoose.model('Instructor', InstructorSchema);


      Here I collect the data from a form



      info=;
      info['instructor_ID']=req.user.id;
      info['instructor_username']=req.user.username;
      info['class_id']=req.body.class_id;
      info['class_code']=req.body.class_code;
      info['class_title']=req.body.class_title;


      This is my data



      module.exports.register=function(info,callback){
      instructor_ID=info['instructor_ID'];
      instructor_username=info['instructor_username'];
      class_id=info['class_id'];
      class_code=info['class_code'];
      class_title=info['class_title'];


      Here is the function



      var query = {username: instructor_username};
      Instructor.findOneAndUpdate(
      query,
      {
      $push: {
      "classes": {
      class_id: class_id,
      class_code: class_code,
      class_title: class_title
      }
      }
      },
      { safe: true,
      upsert: true
      },
      callback
      );


      This is the error



          CastError: Cast to embedded failed for value "{ class_id: '5be353ead45965c74d9e2853 ',n  class_code: 'ART101 ',n  class_title: 'Arts Fundamentals ' }" at path "classes"
      at new CastError (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooseliberrorcast.js:29:11)
      at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:349:19)
      at DocumentArray.cast (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibschemadocumentarray.js:276:17)
      at castUpdateVal (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:365:24)
      at walkUpdatePath (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:165:24)
      at castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibhelpersquerycastUpdate.js:79:18)
      at model.Query._castUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3861:10)
      at castDoc (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3889:18)
      at model.Query.Query._findAndModify (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:3021:19)
      at model.Query.Query._findOneAndUpdate (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_modulesmongooselibquery.js:2732:8)
      at C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:278:20
      at _next (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:102:16)
      at process.nextTick (C:UsersuserDocumentsWeb DevelopmentNodeelearningnode_moduleskareemindex.js:499:38)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)






      node.js express mongoose






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 15:20







      Lenguajes

















      asked Nov 15 '18 at 14:07









      LenguajesLenguajes

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Finally, I changed the



          class_id:{type:[mongoose.Schema.Types.ObjectId]},


          For



          class_id:{type: Schema.Types.Mixed},


          Now it works



          UPDATE:
          My class_id had a white space at the end, I don't know exactly why however I removed it using the trim function. After that, the program works correctly using



          class_id:{type:[mongoose.Schema.Types.ObjectId]} 





          share|improve this answer

























            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%2f53321261%2fcasterror-cast-to-embedded-failed-for-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Finally, I changed the



            class_id:{type:[mongoose.Schema.Types.ObjectId]},


            For



            class_id:{type: Schema.Types.Mixed},


            Now it works



            UPDATE:
            My class_id had a white space at the end, I don't know exactly why however I removed it using the trim function. After that, the program works correctly using



            class_id:{type:[mongoose.Schema.Types.ObjectId]} 





            share|improve this answer






























              0














              Finally, I changed the



              class_id:{type:[mongoose.Schema.Types.ObjectId]},


              For



              class_id:{type: Schema.Types.Mixed},


              Now it works



              UPDATE:
              My class_id had a white space at the end, I don't know exactly why however I removed it using the trim function. After that, the program works correctly using



              class_id:{type:[mongoose.Schema.Types.ObjectId]} 





              share|improve this answer




























                0












                0








                0







                Finally, I changed the



                class_id:{type:[mongoose.Schema.Types.ObjectId]},


                For



                class_id:{type: Schema.Types.Mixed},


                Now it works



                UPDATE:
                My class_id had a white space at the end, I don't know exactly why however I removed it using the trim function. After that, the program works correctly using



                class_id:{type:[mongoose.Schema.Types.ObjectId]} 





                share|improve this answer















                Finally, I changed the



                class_id:{type:[mongoose.Schema.Types.ObjectId]},


                For



                class_id:{type: Schema.Types.Mixed},


                Now it works



                UPDATE:
                My class_id had a white space at the end, I don't know exactly why however I removed it using the trim function. After that, the program works correctly using



                class_id:{type:[mongoose.Schema.Types.ObjectId]} 






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 19:55

























                answered Nov 15 '18 at 16:25









                LenguajesLenguajes

                11




                11
































                    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%2f53321261%2fcasterror-cast-to-embedded-failed-for-value%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

                    Bressuire

                    Vorschmack

                    Quarantine