0
votes

I am currently trying to catch posted values from form inside specific route rule. Since all the other SO posts about this do not work I wanted to ask again.Do you have this sorted out and implemented in your projects? Is there a solution for [email protected]?

this.request.body

Above code inside route rule always returns undefined.

 Router.route('/register', function(){
   console.log( JSON.stringify(this.request.body) );
   //this.render('test',{data:{data:this.request.body.username}})

 });







//SERVER ONLY
if (Meteor.isServer) {
  Meteor.methods({
    'addSong': function(songName) {
      var userId = Meteor.userId()
      songs.insert({
        userId: userId,
        name: songName
      })
    }

  })

  Router.onBeforeAction(Iron.Router.bodyParser.urlencoded({
    extended: true
  }));

}
2
I guess this is for server side can you show us some code? - Mark Uretsky

2 Answers

0
votes

The iron router guide lets us know that this.request and this.response are "NodeJS request and response objects".

If you take a look at some documentation for req.body, you will find that:

By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.

From Iron-router's guide:

IR makes express' body-parser available at Iron.Router.bodyParser.

So there you have it! If you want this.request.body to be populated, you should maybe add:

Router.onBeforeAction(Iron.Router.bodyParser.urlencoded({
    extended: true
}));
0
votes

I have created one file for my controller to maintain code re-usability name as solar.js under controller folder that solar file has my db functionality and pass the request and response as parameter for that file like exports.getSolarInfo = (req,res) => { console.log(req.body) }, here your will get ur body paramter.then manipulate our functionality here then send response like response = { "status" : 0, "result" : "invalid query" } res.end(JSON.stringify(response));

// importing controller const SOLAR = require('./controllers/solar.js');

Router.route( '/solar', function() {
  //setting header type to allow cross origin
  this.response.setHeader( 'Access-Control-Allow-Origin', '*' );
  if ( this.request.method === "OPTIONS" ) {
    this.response.setHeader( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept' );
    this.response.setHeader( 'Access-Control-Allow-Methods', 'POST, PUT, GET, DELETE, OPTIONS' );
    this.response.end( 'Set OPTIONS.' );
  } else {
    SOLAR.getSolarInfo(this.request,this.response);
  }
}, { where: 'server' });