0
votes

I'm just learning how to use Firebase cloud functions written in typescript. I want to set up a cron-job to ping my function every day. And I want the function to iterate through every node in the 'users' node and push the dailyTimeObject to each 'user' node. But what I can't figure out, is if there is a user id wild card I can use to write past the user ids. As you can see I tried {uid} but that just created a new node.

This is my function:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()


export const dailyShiftPush = functions.https.onRequest((request, response) => {
    const pushRef = admin.database().ref('/users/{uid}/dailyTimeObject').push();
    const pushKey = pushRef.key

    const currentTime = Date.now()
    const dailyTimeObject = {
        "time": currentTime,
        "entryId": pushKey
    }
    return pushRef.set(dailyTimeObject)
});

And this is what my firebase realtime database json tree looks like. And I want the {uid} node to be under the actual user id.

{
  "users" : {
    "zVfojjhYUqOzM7hfm2ff8yQtozq2" : {
      "clockIn" : {
        "-LQRpr7wrDAn0kcuicnz" : {
          "clockInTime" : 1541303401051,
          "entryId" : "-LQRpr7wrDAn0kcuicnz"
        },
        "-LQU-_MY8z81yJyUkNJ3" : {
          "clockInTime" : 1541339764229,
          "entryId" : "-LQU-_MY8z81yJyUkNJ3"
        }
      },
      "clockOut" : {
        "-LQRprt3OI465av7sOsF" : {
          "clockOutTime" : 1541303404130,
          "entryId" : "-LQRprt3OI465av7sOsF"
        }
      }
    },
    "{uid}" : {
      "dailyTimeObject" : {
        "-LQW1a1w_P0WaYiZevle" : {
          "entryId" : "-LQW1a1w_P0WaYiZevle",
          "time" : 1541373845702
        }
      }
    }
  }
}

Thanks in advance!

1
Can you please clarify what you want to do. Do you want to automatically create that dailyTimeObject on user creation like some kind of init or you want to use that function once to update user already created in Firebase? Because what you would do is completely different. - Simon Cadieux
You've included a pictures of your code and JSON tree in your question. Please replace those with the actual code and JSON as text, the latter of which you can easily get by clicking the Export JSON link in your Firebase Database console. Having the code and JSON as text makes them searchable, allows us to easily use it to test with your actual code/data and use it in our answer, and in general is just a Good Thing to do. - Frank van Puffelen

1 Answers

0
votes

Your trying to use a wildcard that are used with onWrite, onUpdate and so on. These functions are triggered whenever a path is modified, written, updated, deleted. With those functions you get context that is passed to you, you can then call context.params.(This would be your wildcard, {uid}), look at the signature of those functions.

Now in your case you want to use cron job to call a onHTTPRequest function. In that kind of function you pass the parameters you need thru request yourself, which are optionals, check the signature of that type of function.

When your functions gets called you will have to query Firebase to get UID's of every user, from there iterate and write your dailyTimeObject in each user node. To prevent reading a lot of data, you could have another node with all the UID saved on user creation, this way you would only need to read the UID's and not all the underlying data.

Also depending on your structure, which i still don't get (still don't know why you would write the same data to every users). You could build the ID your self (UID + date ... something like that) and then use multi-path updates to write the data to all user at once instead of looping thru all users node and get a push key for each one.