如何在函数中添加函数
How to add a function to a function
我目前有一个 cron 作业功能,每天早上 7:00 上午发送一条消息,它运行良好,但现在我希望它从 Trello 卡片获取消息并且 Trello 部分运行良好,但是当我把它们放在一起时,它不起作用。有任何想法吗?这是我目前所拥有的:
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'
);
})
您的代码基本上会尝试执行此操作(按此顺序):
- 开始获取 Trello 卡片
- 使用(不存在的)Trello 卡发送嵌入
- 稍后,当取回 Trello 卡片时:登录"Found card: <card name>"
这是因为trello.getCardsOnListWithExtraParams
是一个异步函数。您得到的结果 trelloCard
作为回调函数的参数,但是当您向嵌入添加字段时,您正试图在回调之外访问它。
将其放入您的 cron 函数中:
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)
})
我目前有一个 cron 作业功能,每天早上 7:00 上午发送一条消息,它运行良好,但现在我希望它从 Trello 卡片获取消息并且 Trello 部分运行良好,但是当我把它们放在一起时,它不起作用。有任何想法吗?这是我目前所拥有的:
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'
);
})
您的代码基本上会尝试执行此操作(按此顺序):
- 开始获取 Trello 卡片
- 使用(不存在的)Trello 卡发送嵌入
- 稍后,当取回 Trello 卡片时:登录"Found card: <card name>"
这是因为trello.getCardsOnListWithExtraParams
是一个异步函数。您得到的结果 trelloCard
作为回调函数的参数,但是当您向嵌入添加字段时,您正试图在回调之外访问它。
将其放入您的 cron 函数中:
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)
})