遇到未捕获的诺言拒绝错误,但所有诺言都有捕获块

时间:2019-02-06 04:42:55

标签: javascript promise

我有一个异步函数正在拒绝,但是由于某种原因,即使我对诺言有所了解,我也无法得到错误消息。

相关代码:

const imap = require('imap')

// other code

async startImap() {
  this.server = new imap({
    user: myEmail,
    password: myPassword,
    host: 'imap.gmail.com',
    port: 993,
    tls: true
  })

    this.server.connect()
    this.server.once('ready', () => {
      return this._this.server
    })
}

//some more code

new email('@.', 'true').startImap().then((server) => {
  server.openBox('INBOX', () => {
    server.seq.search('a').then((data) => {
      console.log(data)
    }, (error) => {
      console.log(error)
    })
  })
}, (error) => {
  console.log(error)
})

所有代码:

const mail = require('nodemailer')
const imap = require('imap')
const events = require('events')
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'




class mailServer {
  constructor(transporter, emails, interval, message) {
    this.transporter = transporter
    this.emails = emails 
    this.interval = interval
    this.message = message
  } 
}

class email {
  constructor(email, password) {
    this._this = this
    this.transporter = mail.createTransport({
      service: email.split('@')[1].split('.')[0],
      auth: {
        user: email,
        pass: password
      }
    })
  }

  send(recipient, message, subject = '', nickname = this.email) {
    let mailOptions = {
      from: nickname,
      to: recipient,
      subject: subject,
      text: message
    }
    this.transporter.sendMail(mailOptions, function(error, info){
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent: ' + info.response);
      }
    })
  }

  newServer() {

  }

  async startImap() {
    this.server = new imap({
      user: myEmail,
      password: myPassword,
      host: 'imap.gmail.com',
      port: 993,
      tls: true
    })

      this.server.connect()
      this.server.once('ready', () => {
        return this._this.server
      })
  }

}

new email('@.', 'true').startImap().then((server) => {
  server.openBox('INBOX', () => {
    server.seq.search('a').then((data) => {
      console.log(data)
    }, (error) => {
      console.log(error)
    })
  })
}, (error) => {
  console.log(error)
})

预期结果:错误或数据(参数)

实际结果:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'openBox' 
of undefined
    at email.getData.then (C:\Users\MyName\Desktop\Js_Libraries\Data- 
    comms\email.js:68:10)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
    at process.runNextTicks [as _tickCallback] 
(internal/process/next_tick.js:51:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:804:11)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3) 

0 个答案:

没有答案