Ok, I have this code on the server side:
//login
app.post('/api/users/login',function(req,res){
User.findOne({email:req.body.email}).then(function(user){
if(!user){
return res.status(400).send('email invalid')
}
bcrypt.compare(req.body.password, user.password, function(err,response){
if (response){
user.generateAuthToken().then(function(token){
// this works. I can get that far in the code
res.header('x-auth',token).send(response);
})
} else {
return res.status(400).send('wrong password')
}
})
}).catch(function(e){
res.status(400).send(e);
})
});
and this on the front-end:
var submitLogin = function(){
var body = {
email: document.getElementById('login-email').value,
password: document.getElementById('login-password').value
};
$.ajax({
type : "POST",
url : "/api/users/login",
data : body,
dataType: 'json',
success: function(){
console.log('success')
},
error: function(error) {
// the front-end keeps running this error function instead of the success function.
console.log('error: '+error)
}
});
}
can anyone figure out why the success function never runs?
edit to add: the call works in postman and returns the ‘response’ object and a 200 status