running queries in firebase using async / await

fox picture fox · May 1, 2017 · Viewed 22k times · Source

Appreciating that firebase has added support for promises, is there a way to run a query like the following inside of an async function?:

const eventref = this.db.ref('cats/whiskers');
const value = await eventref.once('value')

Running the above returns a promise for value, I'm hoping to get the json blob that is stored at cats/whiskers.

Answer

Hank Phung picture Hank Phung · Dec 8, 2017

The result of value is a snapshot, we need 1 more step to get the value. This should be like:

const eventref = this.db.ref('cats/whiskers');
const snapshot = await eventref.once('value');
const value = snapshot.val();