javascript - How to submit a sucessful post request using the MEAN stack? -


ok, i'm using yeoman scaffold project. in end want learn scaffold crud. i'm getting stuck on post request. i've picked angular-fullstack generator because i'm comfortable normal angular-generator.

my post request attempts being hulled 400 error while trying submit new object things collection. i'm pretty lost, need see completed code of post request using folder structure.

update: 400 error gone however, req.body.

update2: working answers both in code , answer.

so, using angular-fullstack generator of yeoman, how make post request create awesomething in thing collection of mongo db.

i'm surely missing think of pieces needed make post request successful presented below. direction appreciated.

app/views/partials/main.html

<div class="row marketing">   <div ng-repeat="thing in awesomethings">       <h4>{{thing.name}}</h4>       <p>{{thing.info}}</p>   </div> </div>     <form ng-submit="addawesome()">         <input type="text" ng-model="tempawesome.name">         <input type="submit" class="btn btn-primary" value="add"> </form> 

app/scripts/controllers/main.html

$scope.name = []; $http.get('/api/awesomethings').success(function(awesomethings) {       $scope.awesomethings = awesomethings;     }); $scope.addawesome = function() {       $http.post('/api/awesomethings', $scope.tempawesome).success(function(postdata) {         $scope.awesomethings = postdata;        }).error(function(postdata, status){           console.log(postdata);           $scope.status = status;           console.log(status);       });      }; 

lib/routes.js

  app.route('/api/awesomethings')     .get(api.awesomethings)     .post(api.create); 

lib/controllers/api.js

mongoose.model('thing', thingschema);    exports.create = function (req, res) {    var awesomething = new thing(req.body);   awesomething.save(function(err) {      if (err) return res.json(400, err);    });   }; 

lib/models/thing.js

var thingschema = new schema({   name: string,   info: string,   awesomeness: number }); 

i have hunch mongoose blocking because submission isnt matching schema? ? other thoughts? enter image description here

edit

ok 1 last suggestion based on looking @ other code have. try post instead

$http.post('/api/awesomethings' { name : $scope.name }) 

edit based on code change

change ng-model in form to.

<input type="text" ng-model="name"> 

i think line failing

var awesomething = new thing(req.body); 

what think happening sending object looks in body

{ tempthing: "sometext" } 

when mongoose tries create object dosen't have property called tempthing bails out. can overridden setting {strict:false} on schema if but, if intention set name form start change tempthing name.

old answer

i think /lib/routes.js has typo. have ';' after .get(api.awesomethings)

that's causing .post(api.awesomethings) ignored. remove semicolon , start working.


Comments

  1. Thanks for sharing most valuable information with us.
    Full stack Online Training

    ReplyDelete
  2. Good post and informative. Thank you very much for sharing this good article, it was so good to read and useful to improve my knowledge as updated, keep blogging. Thank you for sharing wonderful information with us to get some idea about that content.
    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore


    ReplyDelete

Post a Comment

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -