【问题标题】:How can i add these values to array?如何将这些值添加到数组中?
【发布时间】:2020-05-21 10:57:48
【问题描述】:

我在添加这些数组时遇到了问题

637301291068030997 => { guildMemberCount: 4,
guildOwnerID: '348832732647784460',
guildOwner: 'Ethical Hacker',
prefix: '.',
guildID: '637301291068030997',
guildName: 'test server 3',
welcomeChannelID: '-' },
(some number) => {other array}

在上面的数组中,我在doc.id 变量中接收到637301291068030997 数字,而其余的则进入doc.data()

我的代码是这样的

var temp = {}
temp.guilds = []                                 // after some lines 
snapshot.forEach(doc => {
        console.log(doc.id, '=>',doc.data());    // output is above array shown 
        temp.guilds.push(doc.id = doc.data())    // output of this line is given below this code
    })

这里是temp.guilds.push 的输出,缺失值是doc.id637301291068030997

{ guilds:
   [ { guildID: '637301291068030997',
       guildName: 'test server 3',
       welcomeChannelID: '-',
       guildMemberCount: 4,
       guildOwnerID: '348832732647784460',
       guildOwner: 'Ethical Hacker',
       prefix: '.' },
     {}   // this missing thing before {} is (some number) also bracket is empty by the way so no worries 
  ] 
}

我该怎么做才能在变量中得到如下所示的输出

  {
    "637301291068030997": [
      {
        "guildMemberCount": 4,
        "guildOwnerID": "348832732647784460",
        "guildOwner": "Ethical Hacker",
        "prefix": ".",
        "guildID": "637301291068030997",
        "guildName": "test server 3",
        "welcomeChannelID": "-"
      }
    ]
  }

将临时文件保存到文件的问题

await fs.writeFileSync ("./data/json/serversettings.json", JSON.stringify(temp), function(err) {
        if (err) throw err;
        console.log('done');
        })

省了这么多

{"guilds":[]}

没有在其中保存任何内容,但 console.log(temp) 给出了正确的输出

【问题讨论】:

  • 你为什么要(在你想要的结果中)"637301291068030997" 有一个 array 作为值?您是否期望该数组中有多个条目?为什么不是没有数组包装的对象?
  • 我同意@trincot。您要使用的数据结构不会为您提供比现有数据结构任何额外的好处。你能解释一下你为什么需要它吗?以后单独使用该对象将变得更加容易。实际上,我之前已经制作了自己的不和谐机器人,并且可以第一手告诉你。
  • 是的,请查看输出,因为此文件将不断使用,因此如果每个服务器位于不同的服务器 ID 中会更容易,这样会更快
  • 您能否在问题中说明snapshot 是什么?您是否在使用 firebase 或其他提供此类对象的 API?

标签: arrays node.js json discord.js


【解决方案1】:

使用doc.id = doc.data(),您将数据分配id 属性。那不可能是你想要的。

我建议根本不创建数组,而是创建一个普通(嵌套)对象。

像这样:

// ...
temp.guilds = {} // plain object, not array
snapshot.forEach(doc => {
    temp.guilds[doc.id] = doc.data();
})

如果snapshot.forEach 实现以异步方式进行回调,那么请确保等到所有回调都完成后再依赖temp.guilds 的内容。承诺可以减轻这项任务。

// ...
let promise = new Promise(function(resolve) {
    let guilds = {} // plain object, not array
    let remaining = snapshot.size; // If firebase, there is this property
    snapshot.forEach(doc => {
        guilds[doc.id] = doc.data();
        remaining--;
        if (!remaining) resolve(guilds);
    });
});
promise.then(function (guilds) {
    // do anything you like with guilds inside this function...
    let temp = { guilds };
    // ...
});

【讨论】:

  • 保存到 json 文件时出现问题,我的代码在上面的 qus 中更新
  • 如果您的guilds 数组看起来是空的,那么您访问它的速度太快了。我不知道snapshot 是什么(你没有告诉),但我猜它的forEach 方法异步调用了回调,所以你需要等待所有的回调都已经完成,然后再做下一步。跨度>
  • 查看补充答案...我假设您使用的是 firebase 快照(?)
猜你喜欢
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 2021-09-13
  • 2016-10-15
  • 2018-11-01
相关资源
最近更新 更多