0

Am working on a project using the mean stack. Before setting up the client side am testing my routes using postman.

http://www.getpostman.com/

Am trying to send a post request to fetch a specific user and a post request to add a user. This is the code....

//cons.js

 'use strict';

var strg = 'mongodb://localhost/users';

module.exports={
   strg:strg
}

//models.js

'use strict';

var mong = require('mongoose'), 
    str = require('./cons');
mong.connect(str.strg);

var pips = mong.model('pips',{
    name: {type: String, required: true, index: { unique: true }},
    pass: {type: String, required: true},
    act:{type: Boolean}
    });

module.exports = {
    pips: pips
}

//user_ctrl.js
var addPips, getPipsById,
    pip = require('./models').pips;

getPipsById = function(req, res){

        /*pip.findOne({jina:req.params.unam}).exec(
            function(err,rez){

            if(err||!rez){
            res.send(err);
        }
        res.json(rez);
    })*/

        pip.findOne({jina:req.body.unam}).exec(
            function(err,rez){

            if(err||!rez){
            res.send(err);
        }
        res.json(rez);
    })
};

addPips = function(req, res){

    pip.create({
        name: req.body.unam,
        pass: req.body.upas,
        act: false
        }, function(err, rez){
        if(err||!rez){
            res.send(err);
        }
        res.send("User account created...");
    })    
};

module.exports = {
    addPips: addPips,
    getPipsById : getPipsById
}

//routes.js

'use strict';

var jada = require('./user_ctrl');

module.exports = function(app){

    //app.get('/api/users/:unm',jada.getUserById);
    app.post('/api/users',jada.getPipsById);
    app.post('/api/users',jada.addPips);

    app.all('/api/*', function(req, res) {
        res.send(404);
    });
};


//server.js

'use strict';

var express = require('express'),
    morgan  = require('morgan'),
    port =process.env.PORT || 3000,
    bodyParser  = require('body-parser'),
    methodOverride = require('method-override'),
    app = express();


app.use(morgan('dev'));             
app.use(bodyParser());              
app.use(methodOverride());          
app.use(express.static(__dirname+"/app"));

require('./config/models');
require('./config/user_ctrl');
require('./config/routes')(app);

app.listen(port);   
console.log('Magic happens on port: '+port);

Am using a post request to fetch a specific user because I am picking information from the form and not the url. When am adding the user. I get an error message: 'validation error name and pass required'. I don't seem to see where this is coming from because I am assigning values from the form to the parameters. Can anyone tell me what am doing wrong? Thank you...

Adwin
  • 195
  • 2
  • 6
  • 21
  • It seems from your code that name and pass are required fields and a validation error for both name and pass occurs. Check whether req.body.unam and req.body.upas have some value or are undefined. One more thing you have defined act as String and you are passing a boolean – Harpreet Singh Jun 08 '14 at 14:42
  • I have corrected the data type issue, thanks. I tested req.body.unam and req.body.upas with other values and found that undefined is being returned. Looking at the server.js things seem to be set up correctly, I have required body-parser. Any suggestions? – Adwin Jun 08 '14 at 19:53

1 Answers1

0

Well, it turns out that Express Body-Parser does not know how to deal with data when the content-type is not set. I found out from here: Express.js req.body undefined

I use postman to test my routes before building the client side. In postman, when simulating form posts I used x-www-form-urlencoded it sets the content-type automatically.

If you are getting undefined form values, try checking if your headers (content-type) are properly set.

Community
  • 1
  • 1
Adwin
  • 195
  • 2
  • 6
  • 21