【问题标题】:Why is data not being saved in the array? | discord.js v13为什么数据没有保存在数组中? |不和谐.js v13
【发布时间】:2022-01-26 21:04:35
【问题描述】:

每当我尝试将数据推送到数组中时,然后在 for 循环之后记录它,它只会打印一个空数组。这是为什么呢?

const discord = require('discord.js');
const db = require('quick.db');
const fs = require('fs');

module.exports = {
    name: 'XYZ',
    run: async (client, message) => {

        var array_V = ['ID1', 'ID2'];
        var snowflakes = [];
        var i = 0;

        message.guild.channels.cache.forEach(channel => {
            if (!channel.isText()) return;
            for (const channel of message.guild.channels.cache.values()) {
                let messages = await channel.messages.fetch()
                messages.each((msg) => {
                  if (msg.author.id === array_V[i]) {
                    snowflakes.push(msg.createdTimestamp)
                  }
                })
            }
        });
    }      
}   

现在输出SyntaxError: await is only valid in async functions and the top level bodies of modules,尽管它已经在标头中声明为异步函数。

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    您将其推送到.then。使用awaitfor 循环来更新它并然后运行下一个代码

    for (const channel of message.guild.channels.cache.filter(c => c.type === "GUILD_TEXT").values()) {
      let messages = await channel.messages.fetch()
      messages.each((msg) => {
        if (msg.author.id === array_V[i]) {
          snowflakes.push(msg.createdTimestamp)
        }
      })
    }
    

    这应该可以工作,因为所有异步都在 for 循环中等待

    【讨论】:

    • 它给了我这个错误SyntaxError: await is only valid in async functions and the top level bodies of modules 虽然它是一个async 函数
    • 这是module.exports中run函数的顶部run: async (client, message) => {
    • 您可以发送您当前的代码吗?
    • 我已将其编辑到帖子中。看看
    • @lopetox612 我已经编辑了答案
    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 2021-11-18
    • 2018-06-08
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多