UnhandledPromiseRejectionWarning: I believe it is after searching on mongodb

0

I believe that this problem is in this part of the code, funny that used a very similar code 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:

  

(node: 7808) UnhandledPromiseRejectionWarning: Unhandled promise rejection (error id: 2): Error: User not found.

I'm a beginner and I can not 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-LogindoAluno</title></head><body><divclass="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{

      }
    });
  });
});
    
asked by anonymous 04.06.2017 / 16:36

1 answer

1

In the 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 imagine 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:

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

    
04.06.2017 / 17:36