skip to Main Content

So I’ve got this HTML form, my idea is to create a new document on my mongo database, that represent the notes given to a teacher based on several criterias, I’m using an array in the note property that contain objects that contains the author of the note for example.

(in the browser I entered these in the form :
name : mathis ||
familyName : de sousa ||
author : mathis de sousa)

I’ve tried using notes[0].author and notes[0][author] without success

    <form action="/teachers" method="post">
        <input type="text" placeholder="name" name="name" required>
        <input type="text" placeholder="familyName" name="familyName" required>
        <input type="text" name="notes[0].author">
        <input type="number" name="notes[0].knowledge">
        <input type="number" name="notes[0].helpfulness">
        <input type="number" name="notes[0].attitude">
        <input type="number" name="notes[0].overallRating">
        <input type="text" name="notes[0].comment">
        <button>Submit</button>
    </form>

the form has to follow this mongoose schema :

const teacherSchema = new mongoose.Schema({

    name: {
        type: String,
        required: true
    },
    familyName: {
        type: String,
        required: true
    },
    notes: [
    {   
        author: {
            type: String,
            required: true
        },
        knowledge: {
            type: Number,
            required: true,
            min:0,
            max:10
        },
        helpfulness: {
            type: Number,
            required: true,
            min:0,
            max:10
        },
        attitude: {
            type: Number,
            required: true,
            min:0,
            max:10
        },
        overallRating: {
            type: Number,
            required: true,
            min:0,
            max:10
        },
        comment: {
            type: String,
            required: false
        }
    }]
});

however, either it output an empty array in notes, either it outputs this issue

C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:3197
    this.$__.validationError = new ValidationError(this);
                               ^

ValidationError: Teachers validation failed: notes: Cast to embedded failed for value "Mathis DE SOUSA" (type string) at path "notes" because of "ObjectParameterError"
    at Document.invalidate (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:3197:32)
    at model.$set (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:1456:12)
    at model.$set (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:1111:16)
    at model.Document (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:166:12)
    at model.Model (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibmodel.js:130:12)
    at new model (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibmodel.js:4770:15)
    at C:UsershddesDesktopjs-udemyESUPcampindex.js:55:24
    at Layer.handle [as handle_request] (C:Usershddesnode_modulesexpresslibrouterlayer.js:95:5)
    at next (C:Usershddesnode_modulesexpresslibrouterroute.js:144:13)
    at Route.dispatch (C:Usershddesnode_modulesexpresslibrouterroute.js:114:3) {
  errors: {
    notes: CastError: Cast to embedded failed for value "Mathis DE SOUSA" (type string) at path "notes" because of "ObjectParameterError"
        at SchemaDocumentArray.cast (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibschemadocumentArray.js:508:19)
        at SchemaType.applySetters (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibschemaType.js:1221:12)
        at model.$set (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:1414:22)
        at model.$set (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:1111:16)
        at model.Document (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:166:12)
        at model.Model (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibmodel.js:130:12)
        at new model (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibmodel.js:4770:15)
        at C:UsershddesDesktopjs-udemyESUPcampindex.js:55:24
        at Layer.handle [as handle_request] (C:Usershddesnode_modulesexpresslibrouterlayer.js:95:5)
        at next (C:Usershddesnode_modulesexpresslibrouterroute.js:144:13) {
      stringValue: '"Mathis DE SOUSA"',
      messageFormat: undefined,
      kind: 'embedded',
      value: 'Mathis DE SOUSA',
      path: 'notes',
      reason: ObjectParameterError: Parameter "obj" to Document() must be an object, got "Mathis DE SOUSA" (type string)
          at EmbeddedDocument.Document (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:113:11)
          at EmbeddedDocument.Subdocument (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibtypessubdocument.js:34:12)
          at EmbeddedDocument.ArraySubdocument [as constructor] (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibtypesarraySubdocument.js:44:15)
          at new EmbeddedDocument (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibschemadocumentArray.js:127:17)
          at SchemaDocumentArray.cast (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibschemadocumentArray.js:502:22)
          at SchemaType.applySetters (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibschemaType.js:1221:12)
          at model.$set (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:1414:22)
          at model.$set (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:1111:16)
          at model.Document (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibdocument.js:166:12)
          at model.Model (C:UsershddesDesktopjs-udemyESUPcampnode_modulesmongooselibmodel.js:130:12),
      valueType: 'string'
    }
  },
  _message: 'Teachers validation failed'
}

Sorry if it’s a dumb question or wrongly formulated, i’m a beginner

Thank you for your help 🙂

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for anyone who responded, I have since solved the issue, I used

    notes[0][knowledge]
    

    The form then returned a string of the entered value (ex:"9")

    I then assigned the value to the correct key such as this :

    const note = teacher.notes.id(noteId);
        note.knowledge = knowledge;
    await teacher.save();
    

    This solved my issue


  2. based on form below:

    <input type="text" placeholder="name" name="name" required>
            <input type="text" placeholder="familyName" name="familyName" required>
            <input type="text" name="notes[0].author">
            <input type="number" name="notes[0].knowledge">
            <input type="number" name="notes[0].helpfulness">
            <input type="number" name="notes[0].attitude">
            <input type="number" name="notes[0].overallRating">
            <input type="text" name="notes[0].comment">
    
    req.body.data = {
    name:"Mathis",
    familyName:"de sousa"
    notes:[
      {
        author:"authorName",
        knowledge:"biology",
        helpfullness:5,
        attitude:5
        overallRating:4.5
        comment:"keep it up"
        
      }
    ]
    }
    let request = req.body.data
    let teacherSchema = new MODELS.teacherSchema()
    teacherSchema.name = req.body.data.name
    teacherSchema.familyName = req.body.data.familyName
    teacherSchema.notes.push(req.body.data.notes)
    await teacherSchema.save()
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search