After creating login credentials in the database, I am facing an issue when trying to use them to log in. The network response for the /api/login endpoint remains stuck on a pending request. Upon checking the response, it seems that the payload data intended to be sent back to MongoDB is not going through.
I attempted to use $q defer promise in the vm.loginuser controller where the call is made, but to no avail. Even using Postman for the login process results in a pending request.
Here is the Angular Controller code:
vm.loginUser = function () {
$http.post('/api/login', vm.userlogin).success(function(response){
console.log('Redirecting to profile');
}).error(function(error){
console.log('Error');
});
};
When I try using .then
instead of .success
, I encounter an error stating "then" is undefined and
localhost:3000/[object%20Object] 404 (Not Found)
The server.js code for calling the login endpoint:
app.post('/api/login', authController.login);
Module: This console.log message appears in the command prompt, and when I include the entire code, the API request gets stuck on pending. Not sure if there's an issue with the code or if MongoDB is taking a long time to return the username and password.
module.exports.login = function (req, res){
res.send('test'); // test successful
User.find(req.body, function(err, results){
if(err){
console.log('Failed to login')
}
if(results && results.lenght ===1){
res.json(req.body.username);
}
})
}
The HTML form for login:
<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>