request.body.email is undefined node js server side using http.get - javascript

I have a problem where request.body.email returns me undefined.
I wrote this code on my controller on my client side:
wish.controller('wishCtrl',['$scope','$http','$cookies',function($scope,$http,$cookies) {
var user={};
user.email = $cookies.get('cookieEmail');
console.log(user.email);
$http.get("http://localhost:3000/wishlist",user).success(function(data){
$scope.wishController = data;
console.log(data);
});
}]);
here I see - user.email ok so there is no problem here.
on my controller on my server side I wrote:
exports.getData = function(request, response) {
userEmail = request.body.email;
console.log(userEmail);
}
which writes me back undefined.
to call this function I have on my server.js (on the server side)
app.get('/wishlist',wish.getData);
any idea how to fix it?

You are making a GET request. There is no request body in a GET request.
If you want to read query string data then parse request.url.
The Angular documentation for get says that the last argument must be a configuration object. So it looks like you aren't even managing to put your data in the URL either. Pass a configuration too.
$http.get("http://localhost:3000/wishlist",user, {})

because i't get function have to pass the email on the client side this way:
$http.get("http://localhost:3000/wishlist/"+user.email)
and the path of the sever should recognize it that way:
app.get('/wishlist/:email',wish.getData);
and int the controller that way:
userEmail = request.params.email;
console.log(userEmail);

Related

How to get data from $.post() request?

I'm trying to send post JQuery request, but I can't get them out in server written using Express.
Right now, I was trying to send data in this way:
Client side
$.post('http://localhost:8080/numberOfAnsweres',{'val': 2})
Server side
app.post('/numberOfAnsweres',(req,res,next)=>{
numberOfAnwers = req.??????
console.log(numberOfAnwers)
})
I don't know which parameter of req should I use.
I'm also not sure if the sending method is good, is there any better way?
You might get the values using express with:
app.post('/numberOfAnsweres',(req,res,next)=>{
var name = req.body.name,
var color = req.body.color;
var value = req.body.val;
})

Function is getting undefined parameter Node.js

I'm working with an application that uses Mongo as DB and node.js. I created a function which is meant to recieve a parameter named orderedBy but my console.log, prints an undefined parameter. Why is this happening? Here's the code.
exports.showAll = function(orderedBy, callback){
var query = {orderedBy: orderedBy}
console.log("Orderer by ===== "+ orderedBy);
var array = models.Orden.find(query).lean().exec(function(err, orders) {
if( orders.length > 0) {
callback({'array' : orders});
//callback({'array' : JSON.stringify(orders)});
}
else {
callback({'error' : "You don't have any orders"});
}
});}
And here i call showAll
app.post('/api/showAll', function(req, res) {
var orderedBy = req.body.orderedBy;
ordenes.showAll(orderedBy, function(found){
console.log(found);
res.json(found);
});
});
Basically, what I want is get an ID and show how many orders that person has. But my response is always "You don't have any orders" mainly because me console log, shows that orderedBy is undefined.
And yes i'm making sure i'm sending the correct ID, simulating it with Postman
your req request object have two properties called
body: when you send a POST/PUT request will be populate by the body of the request
query: when you send a request, (no matters the method) will be populate by the queryString. example http://localhost:3000?orderBy=AAAA
on your server side you can can req.orderBy === 'AAAA'
Thought it was my function being wrong, but it was my parameters. I was stupidly sending form-data, instead of x-www-form-urlencoded in postman. It was never broken, thanks.

Pass Parameter From client side Node js

I am having a basic doubt here, which i can't make it. I have to pass two variables to server side .What i did was
Client Side
$("#send").click(function () {
var getUsrName = $("#text").val();
var getPass = $("#pass").val();
$.post('/connect', {
usr: getUsrName,
pass: getPass
});
});
Server Side
app.post('/connect', function (req, res) {
console.log("req.body : ",req.body.usr)
});
Instead of request.body, I used req.params and req.query. But none of them are working. Its showing undefined.
Thanks
What you're sending to the server is the payload. Just try to console log the whole request coming from the client console.log(req)
from there you will see what you have and again try to console.log(req.payload)
Thanks.

Json returns undefined in nodeJs and AngularJs

I've got an app which makes calls to an api and shows data about that info: first, it takes a username from an input in the client and posts it to the server. In this step if I check if the data has been sent alright to the server everything seems fine.
AngularJs part:
var app=angular.module("app",[])
app.controller("InfoController",['$scope','$log','$http','$filter',function($scope,$log,$http,$filter){
$scope.info = {
summonerName: '',
};
$scope.info.onClick = function(){
$scope.info.summonerName = $scope.info.summonerNameBox;
$http.post('/api/getSummonerName', $scope.info);
$http.get('/api/summonerData')
.success(function(data, status){
$scope.info.display = data;
}).error(function(data,status){
alert("Error: " + status);
});
};
}])
Nodejs:
var summonerName;
var summonerId = '';
app.post('/api/getSummonerName', function (req, res){
summonerName = req.body.summonerName;
console.log("Post getSummonerName " + summonerName);
});
app.get('/api/summonerData', function(req,res){
LolApi.Summoner.getByName(summonerName, function(err, summoner) {
if(!err) {
res.json(summoner);
summonerId = req.body.summoner;
console.log(summonerId);
if(err)
res.send(err)
}
})
});
When I have this data in the server, I make a call to an api for getting this user's info, and everything goes fine as well.
The problem comes when I want to store an specific field from this json. When I want to store it in a variable, everything I get is "undefined" :/ (Once I achieved getting "[Object object]", but can't get it anymore...)
Any idea?
Thank you all.
In my opinion, this isn't the way you should do it.
If you want to do a get on a summoner, you need to just use the get method.
Especially if you're dealing with multiple users.
You might want to pass data in the get request like specified in this answer and ditch the post request:
AngularJS passing data to $http.get request
That way, you're not dependent on the memory and you're able to modularize it :)

Using the PUT method with Express.js

I'm trying to implement update functionality to an Express.js app, and I'd like to use a PUT request to send the new data, but I keep getting errors using PUT. From everything I've read, it's just a matter of using app.put, but that isn't working. I've got the following in my routes file:
send = function(req, res) {
req.send(res.locals.content);
};
app.put('/api/:company', function(res,req) {
res.send('this is an update');
}, send);
When I use postman to make a PUT request, I get a "cannot PUT /api/petshop" as an error. I don't understand why I can't PUT, or what's going wrong.
You may be lacking the actual update function. You have the put path returning the result back to the client but missing the part when you tell the database to update the data.
If you're using MongoDB and ExpressJS, you could write something like this :
app.put('/api/:company', function (req, res) {
var company = req.company;
company = _.extend(company, req.body);
company.save(function(err) {
if (err) {
return res.send('/company', {
errors: err.errors,
company: company
});
} else {
res.jsonp(company);
}
})
});
This mean stack project may help you as it covers this CRUD functionality which I just used here swapping their articles for your companies. same same.
Your callback function has the arguments in the wrong order.
Change the order of callback to function(req, res).
Don't use function(res, req).
Also if you want to redirect in put or delete (to get adress), you can't use normal res.redirect('/path'), you should use res.redirect(303, '/path') instead. (source)
If not, you'll get Cannot PUT error.
Have you been checking out your headers information?
Because header should be header['content-type'] = 'application/json'; then only you will get the update object in server side (node-express), otherwise if you have content type plain 'text/htm' like that you will get empty req.body in your node app.

Categories

Resources