0

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;

}
3
  • Are you sure our req.body object have all data in it. From what I see your mongoose schema sends validation error... And what middleware you using on node side? Commented Sep 11, 2016 at 8:33
  • Can you post your server's main file? Commented Sep 11, 2016 at 8:50
  • @abdulbarik - i have added my full node side api.js Commented Sep 11, 2016 at 9:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

here is my server.js, because already i am using body-parser : var express = require('express'), router = express.Router(), config = require('./server.config'), apiRoutes = require('./app/routes/api'), bodyParser = require('body-parser'), // get body-parser morgan = require('morgan'), // used to see requests mongoose = require('mongoose'), path = require('path'), //sets path from where node runs app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json());
Okay.. Does body-parser included before all routes?
But in post request, I tried to console the body properties, I am getting as undefined console.log( 'family username', family.username ); undefined
Okay.. In angular does your data bindings correct? does vm. form object collect all values from form? maybe some typo in bindings?
see here my cosole : console.log(' called ', req.body ); //getting all properties 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; console.log( 'family username', family.username ); //undefined? not getting
|
0

Put your this code above all the routes in your main server file

 ----------
  ----------
        var app=express();
        app.use(bodyParser.urlencoded({
          extended: true
        }));

  ---------
  ---------

2 Comments

put this line after ` var app = express(); `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.