Unhandledpromiserejectionwarning: I believe it is after mongodb research

Asked

Viewed 788 times

0

I believe that this problem is in this part of the code, funny that below used a code very similar but does not give the error:

UNSOnline.post('/resetpass', (req, res) => {
db.collection('alunos').findOne({email: req.body.email})
.then((doc)=>{
  if(!doc)
  throw new Error('Usuário não encontrado.');

The error message:

(Ode:7808) Unhandledpromiserejectionwarning: Unhandled Promise rejection (rejection id: 2): Error: User not found.

I’m a beginner and I can’t understand this Promise and Rejection...

UNSOnline.post('/resetpass', (req, res) => {
  db.collection('alunos').findOne({email: req.body.email})
  //.then((doc => if(!doc) throw new Error('Usuário não encontrado.');
  .catch(e => console.log(e));

  console.log('Usuário: '+doc._id)
  // Configurações para o email
    var smtpTransport = nodemailer.createTransport({
      service: 'gmail',
      user: '[email protected]',
      pass: 'M@muteP3lud0'
    });
      // Envio do email
    var myhtml = '<!DOCTYPE html><html lang="pt"><head><meta charset="utf-8"><link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.10/semantic.min.css"><script src="https://cdn.jsdelivr.net/semantic-ui/2.2.10/semantic.min.js"></script><title>UNSOnline - Login do Aluno</title></head><body><div class="ui grid container"><div class="row">'
    myhtml = myhtml+'<div class="column"><h1 class="ui header">UNSOnline</h1></div></div><div class="row"><div class="column"><div class="ui message"><h3 class="ui header">Redefinição de Senha</h3><form class="ui form" action="/novasenha" method="post"><div class="field">'
    myhtml = myhtml+'<label>Senha</label><div class="ui left icon input"><input type="password" name="senha1" placeholder="senha"></div></div><div class="field"><label>Senha</label><div class="ui left icon input"><input type="password" name="senha2" placeholder="repita a senha"></div></div>'
    myhtml = myhtml+'<button class="ui button" type="submit">Entrar...</button></form></div></div></div></div></body></html>'
      var mailOptions = {
      sender: '[email protected]',
      to: '[email protected]',      //doc.email,
      subject:'Redefinição de Senha - UNSOnline:'+doc._id,
      html: myhtml
    }
      smtpTransport.sendMail(mailOptions, function(error,response){
      if(error){
        console.log(error);
        res.end("error");
      }else{
        console.log("Mensagem enviada.");
        res.end("Enviado");
      }
  });
});

UNSOnline.get('/login', (req, res) => {
  res.sendFile(__dirname + '/login.html');
});

UNSOnline.post('/validaaluno',(req, res) => {
  db.collection('alunos').findOne({username:req.body.username,senha:req.body.senha})
  .then((doc)=>{
    if(!doc)
    throw new Error('===|| Usuário não encontrado. ||===');

    console.log(doc);

    request('http://94.54.147.150:3201/sso/'+doc.voxyid, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log('>>- Veja a resposta do SSO -<<');
        console.log(body);
        //console.log(error)
        res.redirect(body);
      }else{

      }
    });
  });
});
  • Puts .catch(e => console.log(e)); after that then. Something appears on the console?

  • Referenceerror: doc is not defined, just do a var doc before the function?

  • Okay, there’s the bug. Can you show more code? I see doc in that code you have now, but it looks good. There’s some more doc? That one if(!doc) Are you like this alone? There’s nothing else on that line?

  • Unsonline.post('/resetpass', (req, res) => { db.Collection('students'). findOne({email: req.body.email}) //. then((doc => if(! doc) throw new Error('User not found. '); . catch(e => console.log(e)); console.log('User: '+doc. _id) }); Unsonline.post('/student validator',(req, res) => { db.Collection('students'). findOne({username:req.body.username,password:req.body.password}) . then((doc)=>{ if(! doc) throw new Error('===|| User not found. ||=== '); console.log(doc); }); });

  • I don’t know the right way to show the code?

  • Leave now, but if no one helps I’ll take a look at it soon. You can click [Edit] to edit the question. But we can help format.

Show 2 more comments

1 answer

1


In question you have:

UNSOnline.post('/resetpass', (req, res) => {
  db.collection('alunos').findOne({email: req.body.email})
  //.then((doc => if(!doc) throw new Error('Usuário não encontrado.');
  .catch(e => console.log(e));

  console.log('Usuário: '+doc._id)

I guess what you want is:

UNSOnline.post('/resetpass', (req, res) => {
  db.collection('alunos')
    .findOne({email: req.body.email})
    .then(doc => {
      if(!doc) throw new Error('Usuário não encontrado.');
      console.log('Usuário: ' + doc._id)
    })
    .catch(e => console.log(e));

That is to say:

.then((doc => if(!doc) is syntax wrong, and console.log must be inside {}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.