0

I can't get my query to work when running it in Firebase Functions. It works perfectly on client side but not in Functions. Wonder if there is a way to make it work with Admin SDK as well. I'm I doing something horribly wrong with my query?

Params:

searchQuery = {
  start:
    "[native Date Fri Feb 08 2019 00:00:00 GMT+0200 (Itä-Euroopan normaaliaika)]",
  end:
    "[native Date Fri Feb 08 2019 23:59:59 GMT+0200 (Itä-Euroopan normaaliaika)]",
  cafeIds: ["LI088001"],
  textSearch: "",
  time: { time: "00-24", label: "Kokopäivä" }
}

sort = {
  "column": "created",
  "value": "asc"
}

Query:

let ref = firestoreDB.collection('events')
ref = ref.where('created', '>=', searchQuery.start).where('created', '<=', searchQuery.end)

for (const cafe of searchQuery.cafeIds) {
  ref = ref.where('cafeId', '==', cafe)
}

ref = ref.where(`time.${searchQuery.time.time}`, '==', true)

if (searchQuery.textSearch !== '') {
  ref = ref.where(
    'products.',
    'array-contains',
    lowerCase(searchQuery.textSearch)
  )
}

if (sort.field === 'created') {
  ref = ref.orderBy('created', sort.value)
} else if (sort.field === 'productCount') {
  ref = ref
    .orderBy('created', 'asc')
    .orderBy('productCount', sort.value)
} else if (sort.field === 'total') {
  ref = ref
    .orderBy('created', 'asc')
    .orderBy('total', sort.value)
} else {
  ref = ref.orderBy('created', 'asc').orderBy('eventId', 'asc')
}
const query = await ref.limit(10).get()

When putting this same code to functions which are written in typescript I get following errors:

src/storage/exportCsv.f.ts:25:5 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.
  Property 'id' is missing in type 'Query'.

25     ref = ref.where('created', '>=', searchQuery.start).where('created', '<=', searchQuery.end)
       ~~~

src/storage/exportCsv.f.ts:28:7 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.

28       ref = ref.where('cafeId', '==', cafe)
         ~~~

src/storage/exportCsv.f.ts:31:5 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.

31     ref = ref.where(`time.${searchQuery.time.time}`, '==', true)
       ~~~

src/storage/exportCsv.f.ts:34:7 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.

34       ref = ref.where(
         ~~~

src/storage/exportCsv.f.ts:42:7 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.

42       ref = ref.orderBy('created', sort.value)
         ~~~

src/storage/exportCsv.f.ts:44:7 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.

44       ref = ref
         ~~~

src/storage/exportCsv.f.ts:48:7 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.

48       ref = ref
         ~~~

src/storage/exportCsv.f.ts:52:7 - error TS2322: Type 'Query' is not assignable to type 'CollectionReference'.

52       ref = ref.orderBy('created', 'asc').orderBy('eventId', 'asc')
Eljas
  • 1,187
  • 4
  • 17
  • 37

1 Answers1

1

It looks like you're hitting some TypeScript errors in the Admin SDK. So either you weren't using TypeScript in your client-side JavaScript, or the compiler on the server is more strict. Either way, you'll need to fix each problem, or switch to using plain JavaScript in Cloud Functions too.

There are quite a few errors, but the most common one seems to be:

Type 'Query' is not assignable to type 'CollectionReference'.

28       ref = ref.where('cafeId', '==', cafe)

You define ref here first:

let ref = firestoreDB.collection('events')

Since collection('events) returns a CollectionReference, ref becomes of that type. Then when you later try to assign ref.where('cafeId', '==', cafe) to it, which is of type Query (a ancestor of CollectionReference). So this is a type error.

The solution is to make ref of type Query straight away:

let ref: Query = firestoreDB.collection('events')
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • For some reason even when I manage to fix those errors it does not get any results. Query size stays empty while exactly same query returns 10 items in client side, any ideas why so? Could it be because date objects turns to UTC time standard? – Eljas Feb 10 '19 at 18:41
  • That was the case. I got it fixed by formatting the date object to correct timezone with moment.js. `moment(searchQuery.start).tz('Europe/Helsinki').toDate()` – Eljas Feb 10 '19 at 19:12