rashadb
rashadb

Reputation: 2543

req when posted in Express and req.body when posted from Angular and how to reconcile?

I have the following code:

Module1.js

exports.create = function(req, res) {
     var answer = req.body.key;//this is already declared and works 
//just fine when sent from Angular but this is undefined when sent from
//express


     function something(){console.log(answer)}
}

Module2:

var confusing = require('../Module1Directory/Module1.js');

exports.create = function(req, res){
      var req = {key: value}
      confusing.create(req, res).something();//this is undefined
}

When I post using $http from Angular req.body = {key: value} When I post from Express req = {key: value} and req.body = undefined How do I deal with this difference or what did I miss?

To be clear, I wrote lots of code declaring variables with the req.body format and changing variables now to req only would be very tricky and time consuming as would be porting code between modules (and there'd be all sorts of messy redundancy).

Upvotes: 0

Views: 68

Answers (1)

Luca Colonnello
Luca Colonnello

Reputation: 1456

You redefine req in second module.

var req = {key: value};

Use this instead (obviously this is a bad approach; you wouldn't overwrite the request body if you want grant maintainability):

var req = { body: { key: value } };

Upvotes: 1

Related Questions