Derzeit habe ich Probleme mit Versprechen in mein 'Javascript'-Code. Es wirft weiter "TypeError: res.status (...). Json (...). Catch ist keine Funktion", und ich vermute, dass meine Versprechen irgendwo entlang der Codezeile falsch sind.'UnhandledPromiseRejection' Warnung: Unhandled Versprechen Ablehnung (Ablehnung id: 2): TypeError: res.status (...). Json (...). Catch ist keine Funktion
Dies ist, was ich bisher:
route.js
route.post('/login', function(req, res) {
log.login(req,res).then((post)=>{
res.status(200).json({message: post})
.catch((error)=>{
res.status(400).json({message: error})
})
})
});
und login.js
function login(req,res){
console.log('here', req.body.email, req.body.password)
if (!req.body.email || !req.body.password) {
return Promise.resolve({success: false, msg: 'Please pass email and password.'});
} else {
return Promise.resolve(User.findOne({
'local.email': req.body.email
})).exec().then((user)=> {
if (!user) {
return Promise.reject({success:false, msg: 'Authentication failed. User not found'}); //res.send({success: false, msg: 'Authentication failed. User not found.'});
} else {
// check if password matches
if(user.validPassword(req.body.password)) {
// if user is found and password is right create a token
var token = jwt.sign(user.id, config.secret);
// return the information including token as JSON
return Promise.resolve({success: true, token: 'JWT ' + token});
} else {
return Promise.reject({success: false, msg: 'Authentication failed. Wrong password.'});
}
}
}).catch((errors)=>{
return ({message: "Could not propose login"});
})
}
}
module.exports = {
login
}