How can i add these values to array?

泪湿孤枕 提交于 2020-02-23 07:14:11

问题


I am having trouble while adding these array

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

in the above array i am receiving 637301291068030997 number in doc.id variable and rest is getting in doc.data()

my code is something like this

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
    })

here is output of temp.guilds.push the missing value is doc.id or 637301291068030997

{ 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 
  ] 
}

what can i do so that i will get the output like below in a variable

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

Issue in saving to the temp to the file file

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

saving this much

{"guilds":[]}

not saving anything inside it but console.log(temp) is giving correct output


回答1:


With doc.id = doc.data() you are assigning the data to the id property. That cannot be what you want.

I would suggest to not create an array at all, but a plain (nested) object.

Like this:

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

If the snapshot.forEach implementation makes the call backs asynchronously, then make sure to wait until all call backs have been made before relying on the contents of temp.guilds. Promises can ease that task.

// ...
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 };
    // ...
});


来源:https://stackoverflow.com/questions/60077306/how-can-i-add-these-values-to-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!