2017-12-24 15 views
-2

これは、いくつかの奇妙な理由のために私のコード(非同期機能)

console.log("Main Body Called!"); 

for (var i = 0; i < invitemessages.count; i++) { 
    console.log("For Loop Called!"); 
    if (invitemessages[i].content.startsWith(message.author.id)) { 
     msg = invitemessages[i].content; 
} 

console.log("After for loop called!") 

ですが、私のコンソールに私がMain Body Called!After for loop called!ではなくFor Loop Called!を取得します。これはasync/await関数の内部にあります。

私はforループを呼び出す方法を知っていますか?

EDIT:誰かが全体のことを求めていたので、ここでそれは私がカウントプロパティは、あなたに未定義を与えていると思う

bot.on('message', async message => { 

if (message.author.bot) return; 

if (!message.content.startsWith(".compo")) return; 

var command = message.content.substring(".compo ".length); 

if (command.startsWith("stats")) { 
    var channel = bot.channels.get("394480663643815936") 
    var invites; 
    await channel.guild.fetchInvites().then((collection) => { invites = collection.array(); }) 

    var invitemessages; 
    await channel.fetchMessages().then((collection) => { invitemessages = collection.array(); }); 

    var msg; 

    console.log("Main Body Called!"); 

    for (var i = 0; i < invitemessages.count; i++) { 
     console.log("For Loop Called!"); 
     if (invitemessages[i].content.startsWith(message.author.id)) { 
      msg = invitemessages[i].content; 
     } 
    } 

    console.log("After for loop called!") 

    if (msg == null) { 
     message.channel.send("You have not entered the competition yet! Use `.compo enter`."); 
     return; 
    } 

    for (var i = 0; i < invites.count; i++) { 
     if (invites[i].url == msg.substring(message.author.id.length + 1)) { 
      message.channel.send("Your invite URL is " + invites[i].url + "\n\nYou have currently invited " + invites[i].uses + " people.") 
     } 
    } 
} 

}); 

答えて

0

です。コンソールにログオンしてください。その代わりにlengthプロパティを使用します。

await channel.fetchMessages().then((collection) => { 
     invitemessages = collection.array(); 
     for (var i = 0; i < invitemessages.length; i++) { 
     console.log("For Loop Called!"); 
     if (invitemessages[i].content.startsWith(message.author.id)) { 
      msg = invitemessages[i].content; 
     } 
    } 
    });