Reputation: 65
I have a web app with Angular, backend server with Node.JS. The problem that I having is I press a button on Html, which invoke ng-click recall a function in controller, then, controller will recall a function in Angular services. Finally Angular services will use $http.put to Node.JS server. The problem that I facing is, the parameter "myData" unable to pass to Node.JS Server. Error message from Node.JS: TypeError: Cannot read property 'source' of undefined. My codes are as per following.
html:
<button class="btn btn-default btn-lg col-sm-2 col-sm-offset-2" ng-class="videoVGA" ng-click="myFunction('ABC')" >ABC</button>
Controller:
$scope.myFunction = function (data){
switch(data){
case "ABC":
var myData = new Object();
myData.source = data;
controlProvider.serviceFunction(myData);
break;
Service:
(function(){
function controlProvider($http) {
this.serviceFunction = function(myData){
$http.put("http://localhost:8080/v/switch", myData);
}
}
controlApp.service('controlProvider', controlProvider);
})();
Node.JS:
app.put("/v/switch",function(req,res){
console.log("Printing" + " " + req.body.source);
if(req.body.source === "ABC"){
//Do something
}
});
Upvotes: 0
Views: 79
Reputation: 1158
I'll hazard a guess and say that req.body
itself is undefined. More a problem with node than angular. Do you have the body-parser
module installed? You need to install the body-parser to be able to use req.body
.
Upvotes: 1