getting error while auntheticate
Closed this issue · 8 comments
Hi,
I am new to MEAN stack.I am following your code to learn MEAN.in my code i have used employee instead of user which used in your code. While writing code for authenticate it makes an error which is
events.js:165
throw err;
^
Error: Uncaught, unspecified "error" event. (Incorrect arguments)
at Function.emit (events.js:163:17)
at Immediate. (..../node_modules/mongoose/lib/query.js:2322:23)
at runCallback (timers.js:574:20)
at tryOnImmediate (timers.js:554:5)
at processImmediate [as _immediateCallback] (timers.js:533:5)
Hello, Can you link me to your files/github so I can tak ea look at your application?
Hello, when I ran your application, I registered an employee and then tried to login. It worked for me. I did not receive an error. However, if you look at your api.js file, you can adjust it a bit to get more info about the error. So:
router.post('/authenticate',function(req,res){
Employee.findOne({employeename:req.body.employeename}).select("email employeename password").exec(function(err,employee){
if(err) throw err
instead of throwing the error, you can log it to the command line, so you can get more info. So it would instead look like this:
router.post('/authenticate',function(req,res){
Employee.findOne({employeename:req.body.employeename}).select("email employeename password").exec(function(err,employee){
if(err) console.log(err);
Unfortunately, I am unable to re-create the error. It works for me. However, considering the error you are getting (incorrect arguments), this means that the error may be somewhere else in your application. I'd recommend checking your comparePassword method and ensure it is receiving the password as a parameter. Also check any errors within your database. Try to register and login with a difference user.
Vetrivivek, whenever you get an error that says "Can't set headers after they are sent", this means that you are trying to send back more than one response. You can only send one response per request. So in your case, you are sending back two responses, and that is why you are getting an error. The first response is being sent when the password is not provided:
res.json({success:false,message:'password provided'});
The second response is being sent if there is no valid password:
res.json({success:false,message:'couldnot auntheticate password'});
However, you should not be checking if the password is valid if there is no password.
So in other words, only check if the password is valid if there actually is a password. So it should look like this:
**router.post('/authenticate', function(req, res) {
Employee.findOne({ employeename: req.body.employeename }).select('email employeename password').exec(function(err, employee) {
if (err) throw err;
if (!employee) {
res.json({ success: false, message: 'Could not authenticate employee'});
} else {
if (req.body.password) {
var validPassword = employee.comparePassword(req.body.password);
// Here you should be validating the password, since you have verified that it exists
if (!validPassword) {
console.log('in');
res.json({ success: false, message: 'Could not authenticate password.'});
} else {
console.log('out');
res.json({ success: false, message: 'Employee authenticate'});
}
} else {
res.json({ success: false, message: 'No password provided.'});
}
}
});
});**
Yup you are really awesome man..thank u so much gugui3z24. There is no words to describe u.Thanks a lot