vincenzo199970
vincenzo199970

Reputation: 33

Send JSON from angular.js to node.js

If I send a JSON from frontend angular.js to backend node.js, it give me an error: TypeError: Cannot read property username of undefined. I'm using also Express.

Frontend (Angular.js):

var username_data = $scope.LoginUsername;
var password_data = $scope.LoginPassword;
var data = {username : username_data, password : password_data};

$http.post('/api/login', data)
.then(function (res) {
    //to do...
});

Backend (node.js) with Restful API:

app.post('/api/login', function(req, res) {
    console.log(req.data.username);
    console.log(req.data.password);
});

Upvotes: 1

Views: 355

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

It means data does not exists, just try req.body instead

app.post('/api/login', function(req, res) {
    console.log(req.body.username);
    console.log(req.body.password);
});

Upvotes: 1

Related Questions