0

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
    }
});

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I forgot to include body-parser in my Node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.