Tuesday, 4 April 2017

How can I handle POST results to use mongoose model and then save it to collection?

I did some research on handling POST request with node.js, so that's working now, kind of. I also know how to save a new JSON object to a mongoDB collection with mongoose. I feel like I'm close to the solution but I'm having trouble finding the answer.

The specific problem

Now, I need some help to put these two together.

Handling POST request [ok+-] => converting to JSON object/mongoose object [not okay] => saving it to collection [ok+-]

The controllers code

Here is the code I'm using. The basic features work :

  • handling the fields posted and "saving" them to an object

  • rendering the fields posted (the "fields" object)

the code :

    // controller function for adding a ressource
var post = function(req, res){
    // incoming form
    var form = new formidable.IncomingForm();
    var fields = {};

    console.dir('ceci est bien un post');
    // everytime an field is parsed...
    // This is the place where I should do validation and throw errors if needed
    form.on('field', function (field, value) {
        console.log(field);
        console.log(value);
        fields[field] = value;
    });



    // Once the form is parsed completely
    form.on('end', function () {
        // testing the output
        var ressources = util.inspect(fields, {showHidden: false, depth: null});
        console.dir(util.inspect(fields, {showHidden: false, depth: null}));

        // here save it in mongodb collection
        var ressource = new Ressource(fields);

        // ressource.save(function (err, ressource, isSuccess) {
        //       if (err) {
        //         res.status(400);
        //         res.send('Error occured ' + err);
        //       }
        //       else if (isSuccess === 1) {
        //         res.status(201);
        //         res.send(ressource);
        //       } 
        //       else {
        //         res.status(400);
        //       }
        //     });

        // rendering the page with the results
        res.render('addRessourceView', {
                    title: 'Ressources',
                    fields: ressources
                });
    });
    form.parse(req);
};

And here's the mongoose model :

    var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ressourceModel = new Schema({
  title: { type: String },
  descr_short: { type: String },
  m_visual: { type: Boolean, default: false },
  m_writing:{ type: Boolean, default: false },
  m_moving: { type: Boolean, default: false },
  m_speaking:{ type: Boolean, default: false },
  m_music:{ type: Boolean, default: false },
  age: String,
  minAge: { type: Number }, 
  maxAge: { type: Number }, 
  prerequisite:{ type: Array,
            items: { type: String },
            uniqueItems:  true},
  language: { type: String },
  source: { type: String },
  intra_identify: { type: Boolean, default: false },
  intra_expression: { type: Boolean, default: false },
  intra_comprehension: { type: Boolean, default: false },
  intra_regulation: { type: Boolean, default: false },
  intra_utilisation: { type: Boolean, default: false },
  theme_impulsivity: { type: Boolean, default: false },
  theme_violence: { type: Boolean, default: false },
  inter_identify: { type: Boolean, default: false },
  inter_expression: { type: Boolean, default: false },
  inter_comprehension: { type: Boolean, default: false },
  inter_regulation: { type: Boolean, default: false },
  inter_utilisation: { type: Boolean, default: false },
  details:{ 
    type: Object,
    properties: {
      goals: {  type: Array,
                items: { type: String },
                uniqueItems:  true},
      preparation: String,
      exercices: {  type: Array,
                    items: { type: Object }},
      reflexion: String
    }},
  upload:{  type: Array,
            items: { type: Object }}
  });

module.exports = mongoose.model('Ressource', ressourceModel);

Note : evrything that is "boolean" is a checkbox input

So all these fields are also in the form (i've just not implemented the file upload). So basically what I need help for is :

  • adapting the post fields into something I can use with mongoose and save it
  • maybe some help on how add some validation (like being sure the minAge is smaller than maxAge, or other stuff)
  • and if you have time or would want to, how to handle file uploads

I'm really here to learn, I'm just a beginner and I tried to be as clear as possible with my question, and I hope you guys will be able to help me out !

Thanks, Xogno



via Xogno

No comments:

Post a Comment