0

hey guys may i know how to get data from controller ?

for example

controller.js

function RegisterCtrl($scope, $http, $location) {
    $scope.form = {};
  $scope.submitPost = function() {
    $http.post('/register', $scope.form).
      success(function(data) {
        $location.path('/');
      });
  };

}

routes/index.js

app.post('/register', sessionHandler.handleSignup);

routes/session.js

 this.handleSignup = function(req, res, next) {
        "use strict";
     // when i try to access into the controller by using "data" it said my "data" 
     // is not declare, what should i pass into `var data` = ?? in order to
     // access the data from controller 

        var email = data.email,
         confirmEmail = data.confirmEmail,
         password = data.password,
         confirmPassword = data.confirmPassword,
         firstName = data.firstName,
         lastName = data.lastName,
         penName = data.penName,
         publicUsername = data.publicUsername;
}

2 Answers 2

1

You have to use the express.urlencoded() and express.json() middleware in order to parse body data that was sent via HTTP DELETE / POST / PUT.

After that, there will be a body property on the req object and you can use it like that:

this.handleSignup = function(req, res, next) {
    "use strict";

    var data = req.body,
        email = data.confirmEmail,
        // etc.
};
Sign up to request clarification or add additional context in comments.

1 Comment

hi thanks for your answer, do i have to include this 3 middleware in my app ? app.use(express.bodyParser()); app.use(express.urlencoded()); app.use(express.json())
0

from the success response in your http post you could just assign a scope to it then display it to your partials. Something like this.

$http.post('/register', $scope.form).
  success(function(data) {
    $scope.data = data;
  });

Then from your partials/view

<div>{{data}}</div>

Regards,

1 Comment

hi thanks for reply,i need to pass the data into a function to perform validation after that insert into database rather than display it.

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.