0

I am writing a cloud function that triggers the onWrite event in the real-time database. However, I want to hold the execution of the function for a few seconds and then check the values of a few variables. However, the snapshot that I would receive would be at the instant the function starts to run right? Is there any way I can ask the system to wait once it's triggered, and then after 3 seconds(say) get the data values from my database and process them as required? Or is there anyway I can regenerate a fresh snapshot after 3 seconds and use it?

speedster01
  • 433
  • 3
  • 19
  • Answer on how to get the current value is below. But this sounds like a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), so I'm not sure if this answer solves your underlying problem. – Frank van Puffelen Jul 03 '18 at 13:32

1 Answers1

1

The snapshot that is passed into your function contains the value as it existed when the function was triggered.

If you want to get the current value after a few seconds, you can load it:

snapshot.ref.once("value").then((newSnapshot) => {
  ... in here you can check newSnapshot.val()
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • What does the parameter "value" stand for? Also, to get the snapshot of a parent I should load it as snapshot.ref.parent.once(...)... right? Or is there any other way also? – speedster01 Jul 03 '18 at 13:40
  • The `"value"` is one of the event types you can listen for. This is a fairly basic Firebase Database operation, so I recommend you check it out on https://firebase.google.com/docs/database/web/read-and-write. – Frank van Puffelen Jul 03 '18 at 13:57
  • I did what you said. I implemented this: var number; snapshot.after.ref.parent.once('value').then((newSnapshot) => { number = newSnapshot.child('phone_number').val(); }); console.log(number); However, I am getting number as undefined consistently. Why is that? If I put the logging statement inside the then(...), I get a proper answer though. – speedster01 Jul 03 '18 at 14:33
  • It's because the data is loaded asynchronously. The value of `newSnapshot` is only available *inside* the callback. – Frank van Puffelen Jul 03 '18 at 14:57
  • Yes but I am extracting the value of phone_number and passing it to the outer variable named number right? – speedster01 Jul 03 '18 at 15:00
  • Also, the method setTimeout doesn't seem to be working when I try to put the function on hold for some time. – speedster01 Jul 03 '18 at 15:45
  • Describing your code is not nearly as effective as adding an [MCVE](http://stackoverflow.com/help/mcve) to your question. But it's not about scope, it's about timing: by the time your try to use your variable, the `then()` callback hasn't been run yet. See https://medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93 and https://stackoverflow.com/questions/40481883/in-nodejs-access-variable-outside-callback-like-gloabal-varaible – Frank van Puffelen Jul 03 '18 at 16:30