0

Trying to login using the data I've created which are in the db however when I post the user&pass back to call /api/login endpoint the network response for this endpoint is stuck on pending request. Check the response and it showing the payload data I'm trying to send back to mongo.

I tried putting $q defer promise in vm.loginuser controller where the call is happening but no avail. Even postman can't do a login process its also stuck on pending request.

Angular Ctrl:

vm.loginUser = function () {
        $http.post('/api/login', vm.userlogin).success(function(response){
            console.log('redirect to profile');
        }).error(function(error){
            console.log('err');
        });
    };

also if I use .then instead of .success I get an error "then" of undefined and localhost:3000/[object%20Object] 404 (Not Found)

server.js to call the login endpoint:

app.post('/api/login', authController.login);

Module: this console.log returns on cmd, if I use the full code the api get stuck on pending request, not sure if the code is wrong or mongoDB is just taking long to return me the username and password.

module.exports.login = function (req, res){
   res.send('test'); // is okay 
   User.find(req.body, function(err, results){
    if(err){
        console.log('Fail to login')
    }

    if(results && results.lenght ===1){
        res.json(req.body.username);
    }
 })
}

html:

<input type="text" class="form-control" id="username" 
    placeholder="Username" ng-model="vm.userlogin.username">

<input type="password" class="form-control" id="exampleInputPassword1" 
    placeholder="Password" ng-model="vm.userlogin.password">

<button type="submit" class="btn btn-default" 
    ng-click="vm.loginUser()">Submit</button>
nCore
  • 2,027
  • 7
  • 29
  • 57
  • serverside is nodejs? console.log on client and serverside the success and the error response to see why its happening. – maleeb Jul 24 '16 at 07:16
  • Yeah its nodejs, everything is just console.logs (client & server side) but none of them are returning because of the pending call. if I add a console.log above http request that will return but anything below that doesn't return. – nCore Jul 24 '16 at 07:21
  • added module code for login in my previous post. – nCore Jul 24 '16 at 07:24
  • u have to response data back to the client. res.send("test"); – maleeb Jul 24 '16 at 07:28
  • I put that in my module.exports and its not returning test back, also if I use .then instead of .success I get an error "then" of undefined. and http://localhost:3000/[object%20Object] 404 (Not Found). – nCore Jul 24 '16 at 07:32
  • return the res.send("test");? (return res.send("test");) – maleeb Jul 24 '16 at 08:02
  • res.send('test') works fine, check my edit on module.exports – nCore Jul 24 '16 at 08:05
  • yeah u see it on cmd, that right. but not on client side on the browser console right? so u have to return it to the client like on my code above. return res.send("test"); – maleeb Jul 24 '16 at 08:08
  • I see it in network preview when I submit the form but I don't see it on cmd. – nCore Jul 24 '16 at 08:11
  • Maybe u check this posting here: http://stackoverflow.com/questions/25408243/trouble-with-put-request-using-node-js-express-angular-and-mongodb – maleeb Jul 24 '16 at 08:12

1 Answers1

-1

Can you test this for your Angular login call

$http.post('/api/login', vm.userlogin)
.then(function(success) {

    console.log("SUCCESS");
    console.log(success);

}, function(err) {

    console.log("ERROR");
    console.log(err);

})
.finally(function() {

    console.log("FINALLY");

});
maleeb
  • 890
  • 7
  • 15
  • its similar to my code, but what I found is if I use the full code in my module.exports that's where the call get stuck on pending, see my edit above. Not sure if mongoDB is taking forever to return the objs back or the code is wrong, but it came from a tutorial which worked fine. – nCore Jul 24 '16 at 07:55