1

Here is my data structure of firebase realtime db.
I want to make an API to send toolboxes data in node.js.

enter image description here

And here is the code I made.

router.get('/toolboxes', function(req, res, next) {
  // toolboxes
  toolboxesRef.orderByChild('timestamp').once('value', snapshot => {
    var toolboxes = [];
    snapshot.forEach(childSnapshot => {
      // get toolbox
      var toolbox = {};
      toolbox = childSnapshot.val();
      // add toolbox to toolboxes
      toolboxes.unshift(toolbox);
    })
    res.send(toolboxes);
  })
});

The response now doesn't fit my expectation.
User in toolbox doesn't join the the user table with the u_id.

enter image description here

What I expect is like :

[
    {
        "createdTime": {
            "datetime": "2020/02/23 12:14",
            "timestamp": 1582388083410,
            "fromNow": "a minute ago"
        },
        "description": "Test2",
        "id": "-M0hfUcS6L3wxCnqd2MK",
        "name": "Test2",
        "tags": [
            "網路產業",
            "專案經理",
            "google"
        ],
        "tools": [
            "slack",
            "gitlab",
            "gmail",
            "adobe-after-effect"
        ],
        "user": {
            "avatar": {
                "height": 50,
                "is_silhouette": false,
                "url": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=2993082880704327&height=50&width=50&ext=1584979302&hash=AeQoVqr0x0UwYXuI",
                "width": 50
            },
            "createdTime": {
                "datetime": "2020/02/23 12:01",
                "timestamp": 1582387303217
            },
            "id": "2993082880704327",
            "link": {
                "facebook": "https://www.facebook.com/app_scoped_user_id/YXNpZADpBWEZAiRS1rMHRoMlEwTGJpQmdGWkxxYjFQakxGZAzhsLTRRbnZALUXJ6ZAnN3Rm43MW9VZAnE0NmFJWjZA0ckVsYVpNYkhZAdVJEYkxYU3pUZAEJHT1JISmp1b3YwVE1yaF85cFRmSVRyMzM0QjZANTzcycndD/"
            },
            "name": {
                "firstName": "Chung Ping",
                "fullName": "Tseng Chung Ping",
                "lastName": "Tseng"
            },
            "toolboxes": {
                "-M0hcYsndNhnbx695UAF": {
                    "value": "-M0hcYsndNhnbx695UAF"
                },
                "-M0hfUcS6L3wxCnqd2MK": {
                    "value": "-M0hfUcS6L3wxCnqd2MK"
                }
            }
        },
        "ownerStatus": true
    },
    {
        "createdTime": {
            "datetime": "2020/02/23 12:01",
            "timestamp": 1582387314574,
            "fromNow": "14 minutes ago"
        },
        "description": "Test",
        "id": "-M0hcYsndNhnbx695UAF",
        "name": "Test",
        "tags": [
            "網路產業",
            "專案經理",
            "google"
        ],
        "tools": [
            "slack",
            "gitlab",
            "gmail"
        ],
        "user": {
            "avatar": {
                "height": 50,
                "is_silhouette": false,
                "url": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=2993082880704327&height=50&width=50&ext=1584979302&hash=AeQoVqr0x0UwYXuI",
                "width": 50
            },
            "createdTime": {
                "datetime": "2020/02/23 12:01",
                "timestamp": 1582387303217
            },
            "id": "2993082880704327",
            "link": {
                "facebook": "https://www.facebook.com/app_scoped_user_id/YXNpZADpBWEZAiRS1rMHRoMlEwTGJpQmdGWkxxYjFQakxGZAzhsLTRRbnZALUXJ6ZAnN3Rm43MW9VZAnE0NmFJWjZA0ckVsYVpNYkhZAdVJEYkxYU3pUZAEJHT1JISmp1b3YwVE1yaF85cFRmSVRyMzM0QjZANTzcycndD/"
            },
            "name": {
                "firstName": "Chung Ping",
                "fullName": "Tseng Chung Ping",
                "lastName": "Tseng"
            },
            "toolboxes": {
                "-M0hcYsndNhnbx695UAF": {
                    "value": "-M0hcYsndNhnbx695UAF"
                },
                "-M0hfUcS6L3wxCnqd2MK": {
                    "value": "-M0hfUcS6L3wxCnqd2MK"
                }
            }
        },
        "ownerStatus": true
    }
]

What should I optimize my code if I want to get each toolboxes data but also join the other tables like users and tools etc. Thanks a lot.

  • Firebase doesn't automatically join data from different branches. You will have to write code for that yourself. And since you didn't write any code to load the user data, so the output doesn't include that data. See https://www.youtube.com/watch?v=Idu9EJPSxiY, https://stackoverflow.com/a/25712665, https://stackoverflow.com/a/48228698 and more from these search results for examples of how to do this: https://www.google.com/search?q=firebase+join+javascript – Frank van Puffelen Feb 22 '20 at 17:09

0 Answers0