将sql.each与异步回调

时间:2018-03-09 14:21:53

标签: javascript node.js sqlite asynchronous callback

我正在使用来自npm(sqlite module)的not sqlite3

我需要在数据库的每一行上使用异步函数。但是,任何异步代码都会导致错误。

以下代码段

async function scanDatabase() {
  console.log("Scanning database...");
  sql
    .each(`SELECT * FROM users`, async function(err, row) {
      if (err) console.log(err);
      console.log(row);
      let member = await guild.fetchMember(row.id);
      console.log(member);
      console.log(row.id);
      console.log(member.user.username);

    })
    .then(function() {
      console.log("finished scanning the database");
    });
}

产生以下内容:

Scanning database...
{ id: 119351283999047680,
  first_payment: '2018-03-09T11:26:26.989Z',
  last_payment: '2018-03-09T12:56:19.784Z',
  expires: '2018-03-16T10:26:26.987Z' }
(node:12292) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 2): Error: Invalid or uncached id provided.
(node:12292) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.
finished scanning the database

我得到了未处理的承诺错误,但我不知道如何修复"错误:提供了无效或未缓存的ID。"

0 个答案:

没有答案