angularjs - app.get req.body undefined -
i'm mean stack newb experiencing problems routes, , presumably, parsing: following code gets me req.body: undefined.:
server.js:
var express = require('express'); var app = express(); var bodyparser = require('body-parser'); var mongoose = require('mongoose'); var db = mongoose.connect('mongodb://127.0.0.1/roomem'); var roomie = require('./public/models/roomie.server.model'); app.use(bodyparser.json()); var roomieroutes = require('./public/routes/roomie.server.routes'); roomieroutes(app); module.exports = app; app.listen('3000');
controller.js:
roomem.controller('roomiectrl', ['$scope', '$http', '$location', '$log', function($scope, $http, $location, $log) { $scope.login = function() { console.log($scope.person.meta); // outputs object correctly $http.get('/roomielogin',$scope.person.meta).success(function(response) { console.log(response); // returns null }); } }]);
roomie.server.route.js:
module.exports = function(app) { app.get('/roomielogin', function(req,res) { console.log(req.body); // displays undefined console.log(req.headers); // shows document can json var name = req.body.username; var query = roomie.where({ 'meta.username': name }); query.findone(function(err, doc) { if (err) return console.error(err); console.log(doc); res.json(doc); }); }); }
i have tried , read every post out there this. of time, people placing app.use(bodyparser.json()); line late in script, mine seems @ right place. other queries working right. problematic one. appreciate help.
instead of doing
get
if sending data in body needpost
request.
change -
$http.get('/roomielogin',$scope.person.meta)
to
$http.post('/roomielogin',$scope.person.meta)
&
app.get('/roomielogin', function(req,res)
to
app.post('/roomielogin', function(req,res)
Comments
Post a Comment