typescriptfirebasefirebase-realtime-databasefirebase-admin

How to cast a snapshot value into a dictionary value with typescript?


How can I convert a snapshot value into a dictionary value of type [String: AnyObject]

I am trying to accomplish the bellow line in swift but in Typescript, so that I can loop through the keys. How can I do that? How do I cast a snapshot returned by firebase query in typescript into a dictionary of [string: AnyObject]

let snapValue = snap.value as! [String: AnyObject]

TypeScript query:

const userRef = admin.database().ref('PeopleWhoFollowMe').child(uid)
const fetchAndUpdate = userRef.once('value')
.then(snap => {

Solution

  • The snap in your query is a DataSnapshot type object. Click through that link to read the API documentation for it.

    You can get the raw JavaScript data object of a DataSnapshot using its val() method. Then you can iterate its properties using any normal JavaScript technique that's discussed heavily already on Stack Overflow: Iterate through object properties