简体   繁体   English

如何将 function 添加到 function

[英]How to add a function to a function

I currently have a cron job function to send a message every morning at 7:00 am, and it works perfectly but now I want it to get the message from a Trello card and the Trello part works perfectly, but when I put them together it doesn't work.我目前有一个 cron 作业 function 每天早上 7:00 发送一条消息,它工作得很好,但现在我希望它从 Trello 卡中获取消息并且 Trello 部分工作得很好,但是当我把它们放在一起时不起作用。 Any ideas?有任何想法吗? Here what I have so far:这是我到目前为止所拥有的:

var job = new CronJob('01 15 18 * * *', function() {
        let listID = "5ec3fda8a26c384e44f063bb";
trello.getCardsOnListWithExtraParams(listID, "pos:top",
       function (error, trelloCard) {
        console.log('Found card:', trelloCard[0].name);
})
const wyrEmbed = new discord.RichEmbed()
      .setTitle("❓**WOULD YOU RATHER**❓")
      .addField(
        "**THE WOULD YOU RATHER**", "test: " + trelloCard[0].name)
      .setDescription(
        "🌞 Top o' the mornin’ to ya! All throughout the night, I’ve been preparing a would you rather question for all of you! I can’t wait to see all of the answers you guys think of! "
      )
      .setTimestamp()
      .setFooter("Munchies Ice Cream", client.user.avatarURL)
      .setThumbnail("https://t4.rbxcdn.com/366f4da2e1924b6e4b0816086b485f05")
      .setColor("7ab7e2"); 
        const wyrthing = client.channels.get("688177088573997066")
  wyrthing.send(wyrEmbed)
    console.log('You will see this message every second')
},
  null,
    true,
    'America/New_York'
);
})

Your code basically tries to do this (in this order):您的代码基本上尝试这样做(按此顺序):

  • Start getting the Trello card开始获取 Trello 卡
  • Send the embed using the (non-existent) Trello card使用(不存在的)Trello 卡发送嵌入
  • Later, when the Trello card has been retrieved : Log "Found card: <card name>"稍后,当 Trello 卡被检索到时:记录“找到卡:<卡名>”

This is because trello.getCardsOnListWithExtraParams is an asynchronous function.这是因为trello.getCardsOnListWithExtraParams是一个异步 function。 You're getting the result trelloCard as a parameter to your callback function, but you are trying to access it outside the callback when you add a field to your embed.您将结果trelloCard作为回调 function 的参数,但是当您将字段添加到嵌入时,您试图在回调之外访问它。

Put this in your cron function:把它放在你的 cron function 中:

let listID = "5ec3fda8a26c384e44f063bb";
trello.getCardsOnListWithExtraParams(listID, "pos:top", function (error, trelloCard) {
  console.log('Found card:', trelloCard[0].name);
  const wyrEmbed = new discord.RichEmbed()
    .setTitle("❓**WOULD YOU RATHER**❓")
    .addField("**THE WOULD YOU RATHER**", "test: " + trelloCard[0].name)
    .setDescription(
      "🌞 Top o' the mornin’ to ya! All throughout the night, I’ve been preparing a would you rather question for all of you! I can’t wait to see all of the answers you guys think of! "
    )
    .setTimestamp()
    .setFooter("Munchies Ice Cream", client.user.avatarURL)
    .setThumbnail("https://t4.rbxcdn.com/366f4da2e1924b6e4b0816086b485f05")
    .setColor("7ab7e2"); 
  const wyrthing = client.channels.get("688177088573997066")
  wyrthing.send(wyrEmbed)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM