0

So I'm making an app with firebase and to subscribe all the members of a given group to a specific group topic, I need to create a function that returns an array of all of the groups of a given user.

I have a mongo folder which just returns all the groups where either the "member", "admin" or "owner" field contains their userId, for example mine is "9b8bcd57-06eb-471c-8910-c5b944d02431". However the functions that return the groups you are an owner, admin or member of are separate functions.

I'm quite new to NodeJs and haven't quite grasped promises yet. But I found an example online

Here's what I've tried :


var p1 = new Promise ((resolve, reject) => { resolve( myMongo.retrieveDocuments('groups', { member: userId }) ) })
var p2 = new Promise ((resolve, reject) => { resolve( myMongo.retrieveDocuments('groups', { owner: userId }) ) })
var p3 = new Promise ((resolve, reject) => { resolve( myMongo.retrieveDocuments('groups', { admin: userId }) ) })

return Promise.all([p1, p2, p3])

}

This fetch returns an array with a structure like so.

{
[
    [
        {
            "_id": "60e8341fe765650015db561f",
            "groupName": "Club One",
            "owner": [
                "0bcbb92d-6276-4118-8576-9d5f5c4ed43b"
            ],
            "timestamp": 1625830382197,
            "premiumStatus": false,
            "member": [
                "f2171431-627e-47a3-a65f-4abf48d361b6",
                "9b8bcd57-06eb-471c-8910-c5b944d02431"
            ],
            "admin": [],
            "pending": [],
            "description": ""
        },
        {
            "_id": "6117beac025a040016655483",
            "groupName": "Club 2",
            "owner": [
                "c5c921b5-3ce7-450d-bf39-af027d07f5a2"
            ],
            "timestamp": 1628945997774,
            "premiumStatus": false,
            "member": [
                "9b8bcd57-06eb-471c-8910-c5b944d02431"
            ],
            "admin": [],
            "pending": []
        }
    ],
    [
        {
            "_id": "611e613e6d85ac0016f57e5d",
            "groupName": "People",
            "owner": [
                "9b8bcd57-06eb-471c-8910-c5b944d02431"
            ],
            "timestamp": 1629380919105,
            "premiumStatus": false,
            "member": [],
            "admin": [],
            "pending": []
        }
    ],
    [
        {
            "_id": "60f6dc43be54fb0015c16634",
            "groupName": "UCD",
            "owner": [
                "79154a25-d747-441f-a8e3-4f06a2fbdaac"
            ],
            "timestamp": 1626790968639,
            "premiumStatus": false,
            "member": [],
            "admin": [
                "2c7c2bc9-e1c3-4665-bcd9-2844fae6ac93",
                "f8b4c3b7-0df8-4a14-a1fb-41c363337653",
                "ea4d3222-caa2-40d5-80d8-54ce9a360c2b",
                "9b8bcd57-06eb-471c-8910-c5b944d02431"
            ],
            "pending": [],
            "description": "Diamond by 2022"
        },
        {
            "_id": "6116d1767e80f300166368e7",
            "groupName": "Club",
            "owner": [
                "91dfdca2-6c2e-4784-91ed-d8ad4ffd8e25"
            ],
            "timestamp": 1628885342939,
            "premiumStatus": false,
            "member": [],
            "admin": [
                "9b8bcd57-06eb-471c-8910-c5b944d02431"
            ],
            "pending": [],
            "description": "Ahhhhhhh"
        }
    ]
]

As you can see, this function returns all the promises together in one large parent array. My problem is that I need for the inner array brackets ('[' & '],') to disappear. I've tried split. But i'm not sure it works in node, and I've searched this site and many other ones for answers. So far I've heard of a method called "Flattening an array" but haven't found any proper examples.

Just to conclude.

Here's what I have :

[
   [ thing1, thing2, thing3],
   [ thing4, thing5, thing6],
   [ thing7, thing8, thing9]
]

And here's what I need :

[ thing1, thing2, thing3, thing4, thing5, thing6, thing7, thing8, thing9 ]
  • 1
    You can flatten the array by either spreading them, or using [`Array.prototype.flat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat). – Terry Aug 20 '21 at 12:02
  • 1
    I believe you just want to [flatten an array of arrays](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays). Besides, I guess `myMongo.retrieveDocuments` is already a Promise, so you can get rid of all the `new Promise ((resolve, reject) => { resolve(` litterature. You're only wrapping a Promise inside another Promise. – Jeremy Thille Aug 20 '21 at 12:04

1 Answers1

1

You want Array.prototype.flat(), which is available as of Node 11.

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

The default depth is 1, so you don't need to pass this argument in.

Promise.all([p1, p2, p3]).then(nested_data => nested_data.flat());
romellem
  • 5,792
  • 1
  • 32
  • 64