I have this function which accepts a DataSnapshot
as an argument.
My current work around was, I still have to know beforehand, what keys I have on my firestore document.
This is really not efficient enough, I would like to figure out a way to actually retrieve all fields that are reference data types without knowing the keys beforehand. That would make this Promise
more useful, for instance you can just pass in any DataSnapshot
and it will resolve into a document with the referenced documents appended.
getDenormalizedFields = (data: Object): Promise<any> => {
const denormalizeKeys = ['refOne', 'refTwo', 'refThree'];
const denormalizationPromise = new Promise(async (resolve: Function) => {
const denormalizations = await denormalizeKeys.map(
async (key: string) => {
const field = await data[key].get();
const fieldData = await field.data();
return { [key]: fieldData };
},
);
const promisedDenormalizations = await Promise.all(denormalizations);
const denormalized = Object.assign({}, ...promisedDenormalizations);
resolve(denormalized);
});
return denormalizationPromise;
};