0

I created a function to iterate through all fields of a DataSnapshot.

Here are my imports

import type {
  DataSnapshot,
  DocumentRef,
  DocumentSnapshot,
} from 'react-native-firebase';

the function is below:

getRefData = (data: typeof DataSnapshot) {
   return new Promise(async (resolve: Function) =>{
      const refs = await Object.keys(data).map((key: string) => {
         const field = data[key];
         if (field instanceof DocumentRef) {
             /// LOGIC HERE
         }
      });

   });
};

I'd actually like to add some logic if field is a DocumentRef. It is even undefined, see error below:

Unhandled rejection is {promise: Promise, reason: ReferenceError: DocumentRef is not defined
    at blob:http://localhost:8081/68a9c3b0-2327-429a-b5c7…}

Is there a different or straightforward way to do this? What could've gone wrong?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Jojo Narte
  • 2,767
  • 2
  • 30
  • 52
  • javascript won't see anything that is imported as type. All type imports and information is getting fully stripped down. You need to figure out the type of field based on information that is in it, rather then checking against its type. Checkout this example: https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=react&targets=&browsers=&builtIns=false&debug=false&experimental=false&loose=false&spec=false&code_lz=GYVwdgxgLglg9mABMOcAUCCmAuRBDMATwBpEoB3OXMEAWwCNMAnUqACyc0wH4BKXAM5QmMMAHNEAbwC-QA&playground=false&stage=0 – Adam Jul 14 '18 at 19:27

1 Answers1

0

I think there is no straight-forward way yet to do this in JavaScript as it is hard to verify using types, so I decided I'll just make a change in my Firestore structure by adding object named refs which would contain all references, that would make it easier for me to iterate through all references as I'm sure everything under refs field are DocumentRefs.

Jojo Narte
  • 2,767
  • 2
  • 30
  • 52