Reputation: 5101
I am trying to create a mean
application. as a sample, if I post the request through postman
the data created at mlab
.
in case if I post the same using $http
way, it's not working getting the error as :
{
"message": "Family validation failed",
"name": "ValidationError",
"errors": {
"username": {
"message": "Path `username` is required.",
"name": "ValidatorError",
"properties": {
"type": "required",
"message": "Path `{PATH}` is required.",
"path": "username"
},
"kind": "required",
"path": "username"
},
"password": {
"message": "Path `password` is required.",
"name": "ValidatorError",
"properties": {
"type": "required",
"message": "Path `{PATH}` is required.",
"path": "password"
},
"kind": "required",
"path": "password"
}
}
}
and the node with mongoose :
.post(function( req, res ){
var family = new Family();
family.username = req.body.username,
family.password = req.body.password,
family.familyLeader = req.body.familyLeader,
family.husband = req.body.husband,
family.wife = req.body.wife,
family.kids = req.body.kids;
family.save(function( err, newFamily ) {
if( err ) {
if ( err.code == 11000) {
return res.json({ success: false, message: 'A user with that username already exists. '});
}
else {
return res.send( err );
}
}
res.json({ message: 'Family created!', newFamily: newFamily });
});
})
here is my angular code :
vm.createNewFamily = function() {
$http({
method : 'POST',
url : '/api/family',
data : vm.form,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).success( function ( data ) {
console.log('retured!', data );
})
}
my full api.js ( node )
var Family = require('../models/model_family');
module.exports = function( app, express ) {
var apiRoute = express.Router();
apiRoute.use(function( req, res, next ) {
console.log( 'some one using the app!' );
next();
})
apiRoute.get('/', function( req, res ) {
res.json({"namea" : "Arif"})
});
apiRoute.route('/family')
.get(function( req, res ){
res.send('family get processing');
})
.post(function( req, res ){
var family = new Family();
family.username = req.body.username,
family.password = req.body.password,
family.familyLeader = req.body.familyLeader,
family.husband = req.body.husband,
family.wife = req.body.wife,
family.kids = req.body.kids;
family.save(function( err, newFamily ) {
if( err ) {
if ( err.code == 11000) {
return res.json({ success: false, message: 'A user with that username already exists. '});
}
else {
return res.send( err );
}
}
res.json({ message: 'Family created!', newFamily: newFamily });
});
})
return apiRoute;
}
Upvotes: 0
Views: 1529
Reputation: 3411
EDIT
If you only have problems with username then check you angular data bindings. Im thinking you have typo somewhere like this
<input ng-model="useranme">
Hope this helps.
Upvotes: 1
Reputation: 6242
Put your this code above all the routes
in your main server file
----------
----------
var app=express();
app.use(bodyParser.urlencoded({
extended: true
}));
---------
---------
Upvotes: 0