Angular Firestore – Get document ID as a property to use it again in an other function – Typescript

Issue

So here is my code :

getOutfitsCollectionData(): Promise<any> {
    let outfitsRef  this.afStore.collection('outfits');
    let allOutfits  outfitsRef.snapshotChanges().pipe(
        map(actions>{
            return actions.map(a>{
                const id  a.payload.doc.id;
                return id
            })
        })
    )
    let outfitsID   allOutfits.forEach(outID>{
        console.log(outID)
        return outID
    })
    return outfitsID
}
}

When I do this part

let outfitsID   allOutfits.forEach(outID>{
        console.log(outID)
        return outID
    })

The console is giving me this :

console perfect

It’s exactly what I want, I just want to rename the array and set it as the result of my function but every console.log I put under these lines doesn’t do anything so it seems that every line under isn’t doing anything.

If I’m not clear, tell me guys !

What I’m trying to do is get each documents id of my collection to use it in an other function on the same tab.

I think that it might be easier to create a service but I don’t really know how to do it.

Please don’t make me ban one more time, I’m just trying to learn … It won’t be really smart to send me to a really general subject.

I’m not just trying to have pro that give a code that works, I want to understand and get better.

Thanks for your help guys !

( BTW sorry for my bad English, I’m French 🙂 )

So just a little edit :

I tried this code, I don’t really understand why when I console.log allOutfits, I’m not getting the good value :

async getOutfitsCollectionData(){
    let outfitsRef  this.afStore.collection('outfits');
    let allOutfits  await outfitsRef.get().toPromise().then((step)>{
        return step.forEach(doc>{
        if (!doc.exists){
            console.log('Zut !')
        }else{
            console.log(doc.id)
            return doc.id
        }
    })
})
.catch(err>{
    console.log('Error getting outfits', err)
})
console.log(allOutfits)
return allOutfits
}

Solution

I’ve finished my app long time ago, but I think that might interest.

I finally decided to learn typescript lol.

You can’t get a value of a promise since it’s not return, that was what I was trying to do.

Just call your function and .then do what you want ^^.

By the way, I needed to map my data as said by Lim Shang Yi

Answered By – jmauclair

Leave a Comment