0

I'm trying to make a POST request from Angular Factory to Node.

Angular Factory -

    function saveUser(userObject){
        var createUser = $http({
            method: 'POST',
            url: 'CreateUser',
            data: userObject,
            headers: {'Content-Type': 'application/json'}
        });

        return createUser.then(callSuccess, callError);
    }

Node-

function create(){
    app.post('/CreateUser', urlEncodedParser, function(request, response){
        var userData = {
            firstName : request.body.firstName,
            lastName : request.body.lastName,
            email : request.body.email,
            password : request.body.password,
            role : request.body.role
        };
        console.log(request);
        console.log(userData);

       dbOpperations.saveData(userData, 'UserTable');
    });
}

The call is made but I get response.body = {}

5
  • could be due to the reason that you're not returning any desired data from 'node' Commented Aug 8, 2016 at 6:10
  • it seems that you are not returning anything from the POST request. Try to add response.send('User is created') in the success callback of dbOperations . Commented Aug 8, 2016 at 6:11
  • Does your console.log() works ? Commented Aug 8, 2016 at 6:17
  • Yes, console.log() works. I'm able to see the 'request.body' as empty object. Commented Aug 8, 2016 at 7:32
  • Sorry for the mistake, I'm getting empty object for request.body (instead of response.body). Additionally when I'm using Postman tool it works, but doesn't works when called from code. Commented Aug 8, 2016 at 7:45

2 Answers 2

1

You are getting an empty response because you are not returning anything from node.

please use response.send()

response.send('success')
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks all for your help.

I'm able to POST the data and save on db.

I missed to use app.use(bodyParser.json());

Comments

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.