【问题标题】:Return statement not awaiting返回声明不等待
【发布时间】:2021-05-26 22:42:26
【问题描述】:

当执行return truereturn false 时,它不会await 那些结果并返回undefined。 但是它确实正确地执行了if 语句,只是返回truefalse 不起作用

在登录row.channelOwner 之前先登录undefined,这让我觉得它没有等待返回。

(channel为频道ID,user为用户ID)

client.on('message', async message => {
    if (!message.content.startsWith('=test') || message.author.bot) return;
    const args = message.content.slice('=test'.length).trim().split(' ');
    
    var result = await cowner(args[0],args[1])
    console.log(result)
})

cowner = async function(channel, user) {
    let query = `SELECT channelOwner FROM voice WHERE channelID = ${channel}`

    db.each(query, [], async (err, row) => {
        if (err) {
            console.log(err);
        }

        console.log(row.channelOwner)
        console.log(user) // row.channelOwner and user is the same

        // It logs reached if and reached else so the if statement is working
        if (user === row.channelOwner) {
            console.log('reached if')
            return true;
        } else {
            console.log('reached else')
            return false;
        }
    })
}

【问题讨论】:

  • 您正在从 db.each 中使用的匿名函数返回。但是您没有从 cowner 函数返回任何内容,因此它返回未定义。异步也是不必要的,因为你没有在等待任何东西。
  • 使用 asyc/await 不会将回调转换为 Promise。请改用 promise 构造函数。
  • @Timjime db.each 返回什么?
  • 所以如果这是sqlite3db.each 会遍历每一行,但仍然不会改变回调不返回任何内容的事实。相反,您可能想要使用db.all 甚至db.get,如果保证结果只有零或一行并将其包装在Promise 中。请阅读此答案:stackoverflow.com/questions/46994203/…

标签: javascript node.js async-await discord discord.js


【解决方案1】:

(原答案已删)

如果这是 SQLite3,那么使用each 来查找用户是否是频道所有者有点复杂。我建议使用db.all 甚至db.get (因为它似乎只返回零或一行,而不是更多),同时将它们包装在Promise 中,如下所示:

如果使用db.get()

cowner = async function(channel, user) {
  let query = `SELECT channelOwner FROM voice WHERE channelID = ${channel}`

  const row = await new Promise((resolve, reject) => {
    db.get(query, [], (err, row) => {
      if (err) {
        console.log(err);
        reject(err)
      }

      resolve(row)
    })
  })

  console.log(row.channelOwner)
  console.log(user) // row.channelOwner and user is the same

  // It logs reached if and reached else so the if statement is working
  if (user === row.channelOwner) {
    console.log('reached if')
    return true;
  } else {
    console.log('reached else')
    return false;
  }
}

如果使用db.all()

cowner = async function(channel, user) {
  let query = `SELECT channelOwner FROM voice WHERE channelID = ${channel}`

  const rows = await new Promise((resolve, reject) => {
    db.all(query, [], (err, rows) => {
      if (err) {
        console.log(err);
        reject(err)
      }

      resolve(rows)
    })
  })

  // Iterate through the resulting rows and check if there is one row which channelOwner matches the user
  const isChannelOwner = rows.some(row => user === row.channelOwner)

  // It logs reached if and reached else so the if statement is working
  if (isChannelOwner) {
    console.log('reached if')
    return true;
  } else {
    console.log('reached else')
    return false;
  }
}

如果您想继续使用each,请在此处阅读答案:sqlite3 - promise for asynchronous calls

【讨论】:

  • 这返回Database {filename: 'voice.db', mode: 65542} 基本上只是我认为的所有数据库信息。我使用 sqlite3
  • 我已经更新了答案并删除了原始文本和代码,因为我不知道我们指的是sqlite
猜你喜欢
  • 2020-11-18
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 2021-12-01
相关资源
最近更新 更多