0

I used this tutorial to make a login page. My angular code looks like this.

app.directive('ensureUnique', ['$http', function($http) {
    console.log("In validate");
    return {
        require : 'ngModel',
        link : function(scope, ele, attrs, c) {
            scope.$watch(attrs.ngModel, function() {
                $http({
                    method : 'POST',
                    url : '/email/check/' + scope.$eval(attrs.ensureUnique),
                    data : { 'field' : attrs.ensureUnique}
                    }).success(function(data, status, headers, cfg){
                        c.$setValidity('unique', data.isUnique);
                    }).error(function(data, status, headers, cfg) {
                        c.$setValidity('unique', false);
                    });
            });
        }
    }
}]);

and my routes file i.e index.js is like this.

app.post('/email/check/:email', function(req, res, next){
    console.log(req.query);
});

Now i want to see this data on my Node application, so that I can verify if my email Id is unique. But I am unable to see it. It always gives me a blank object. I consulted many SO post and docs but couldn't find any answer. What is the error here? What am I missing?

3
  • If you check your network tab, do you see the email being added to the URL in the post? Commented Sep 21, 2015 at 21:13
  • It says undefined in my networks tab. Like /email/check/undefined Commented Sep 21, 2015 at 21:34
  • and if I remove scope.$eval from the url it says /email/check/email. Which clearly indicates that attrs.ensureUnique does not have the data to be sent. How do I send the data and not the field name Commented Sep 21, 2015 at 21:38

1 Answer 1

1

That's because request.query is for the querystring content, and your email is being passed as part of the URL. Try this:

console.log(req.params.email);

Also, because you're passing the field name as data, if you're using bodyParser you could read it like this:

console.log(req.body.field);

Update based on your comment: apparently, your directive code is not without fault. If your directive attaches to an input element (this is just an assumption) then the value of that element is in the ele argument. So, your url should look like this:

url: '/email/check/' + ele.val(),
Sign up to request clarification or add additional context in comments.

2 Comments

req.body.email gives me the field name i.e email and req.params.email gives me undefined. Which clearly implies that attrs.ensureUnique does not have a value, set How i ensure that I am sending the data?
Well it didn't work exactly. But I got a workaround. I updated the data field with ele.val() instead of in the url field. Works like a charm. Thanks a ton. If possible could you update your answer.

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.