小编Lou*_*p G的帖子

使用 async/await 函数通过 Imap 检索电子邮件

当我尝试使用下面的代码(使用异步函数)通过​​ imap 检索电子邮件时,我收到以下控制台输出/错误:

收件箱:未定义

/Users/mainuser/node_modules/imap/lib/Connection.js:432 cb(err, self._box); ^ 类型错误:cb 不是函数

var Imap = require('imap');
var inspect = require('util').inspect;

var imap = new Imap({
  user: 'mymailname@mail.com',
  password: 'mymailpassword',
  host: 'imap.mail.com',
  port: 993,
  tls: true
});

const openInbox = async () => {
  try {
    const inbox = await imap.openBox('INBOX', true)
    return inbox
  }catch(error){
    console.log("Error: "+ error)
  }
}

imap.once('ready', () => {
  console.log('ready')
  openInbox()
   .then(inbox => console.log('Inbox: ' + inbox))
});

imap.connect()
Run Code Online (Sandbox Code Playgroud)

但是,我可以打开收件箱并使用嵌套回调输出收件箱对象,如下所示:

imap.once('ready', () => {
  imap.openBox('INBOX', true, (err, inbox) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js async-await

3
推荐指数
1
解决办法
3511
查看次数

标签 统计

async-await ×1

javascript ×1

node.js ×1