ktm125
ktm125

Reputation: 452

Cloud Functions & Firestore: Iterate over document.data

The database structure looks like this:

User {id}
    Settings (Collection)
        device_uids(document)
            {device_uid_1}: Boolean
            (...)
            {device_uid_n}: Boolean

I want to get the document and access all of the device_uids within that document. I tried like this, however the console logs, that forEach is not definded:

const settings_ref = admin.firestore().collection('User').doc(uid).collection('Settings').doc('device_uids');
    settings_ref.get()
    .then(snap =>{
        let uids = snap.data();


        uids.array.forEach(element => {
            let device = element.key;
            if(device != device_uid){
               //GO ON
            }
        });

    })

How can I access the values individually?

Upvotes: 4

Views: 3388

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

You don't have a field called array in your document, so uids.array will always be undefined. If you just want to iterate all the properties of the document, it's just like iterating all the properties of a plain old JavaScript object:

const data = snap.data();
for (const key in data) {
    const value = data[key];
    // now key and value are the property name and value
}

Upvotes: 6

Related Questions