用 for 循环填充 RichEmbed

Filling a RichEmbed with a for loop

对于我的 Discord 机器人,我正在从 API 中获取数据 (json)。我正在使用 a for 循环遍历所有 json 数据,并希望对找到的每条数据使用 .addfield()。不幸的是,这是行不通的。我该如何解决这个问题?

这是我当前的代码:

let search = json.search
let richembed = new Discord.RichEmbed()
  .setTitle("Title")
  .setDescription("Description")
  for (i in search) {
    let title = search[i].title
    let snippet = search[i].snippet
    .addField(title, snippet)
  }
  .setTimestamp()
  .setFooter("bot.user.username", bot.user.avatarURL)

这是来自 API 的 JSON 响应示例。

"search": [{
    "ns": 0,
    "title": "Monkey",
    "snippet": "text here",
    "timestamp": "2019-03-19T04:18:52Z"
  },
  {
    "ns": 0,
    "title": "The Monkey",
    "snippet": "text here",
    "timestamp": "2018-12-31T14:40:16Z"
  },
  {
    "ns": 0,
    "title": "Monkey see, monkey do",
    "snippet": "text here",
    "timestamp": "2019-01-16T00:12:51Z"
  }]

您不能在 RichEmbed Builder 中执行 for 循环。您必须按照以下代码执行此操作:

let search = json.search    
let richembed = new Discord.RichEmbed()
    .setTitle("Title")
    .setDescription("Description")
    .setTimestamp()
    .setFooter("bot.user.username", bot.user.avatarURL);

for (i of search) {
  let title = i.title
  let snippet = i.snippet
  richembed.addField(title, snippet)
}