0

I am trying to query for all documents before a given timestamp, but that timestamp may not exist at the time of the query. I keep getting this error.

FirebaseError: Function Query.where() requires a valid third argument, but it was undefined.

I've looked at using promises, async and await, but I'm still a bit lost.

var myTimeStamp; //might not have a value yet

function loadTimeStamp(){...};

loadTimeStamp(); //function is called give myTimeStamp a value

//firebase onSnapshot call throws error 
ref.where('timestamp', '<', myTimeStamp).onSnapshot(snapshot =>{ ... }); 

I'm probably missing something very simple. Any help is greatly appreciated!

Jake Xia
  • 81
  • 9
  • I think you should show exactly what you're doing to give myTimeStamp a value. Abbreviating it as you have it now removes a bunch of very important details. – Doug Stevenson Aug 21 '19 at 20:14

1 Answers1

0

If the loadTimeStamp() function is asynchronous (e.g. loading from a database) then you will need to use promises or async/await to first get the value before running your query.

You may also want to check if your loadTimeStamp() function does return a correct value ever (add console logs or other testing)

assuming both the function is asynchronous and working something more along the lines of:

async function queryCollectionByTimestamp(){
  const myTimeStamp = await loadTimeStamp()
  const ref = db.collection('whatever')
  const query = ref.where('timestamp','<',myTimeStamp)
  const snapshot = await query.get()
  const data = snapshot.map(doc=>doc.data())
  return data
}

chrismclarke
  • 1,995
  • 10
  • 16