Exploring disparities between the Client SDK and Admin SDK in conducting firestore queries

I am encountering difficulties with my query while running it in Firebase Functions. It functions perfectly on the client side, but fails to work in Functions. I am curious if there is a way to modify it to function with Admin SDK as well. Am I making any critical errors in my query?

Parameters:

searchQuery = {
  start:
    "[native Date Fri Feb 08 2019 00:00:00 GMT+0200 (Eastern European Standard Time)]",
  end:
    "[native Date Fri Feb 08 2019 23:59:59 GMT+0200 (Eastern European Standard Time)]",
  cafeIds: ["LI088001"],
  textSearch: "",
  time: { time: "00-24", label: "Full day" }
}

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 I try to implement this exact code in TypeScript functions, I encounter the 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')

Answer №1

It appears that you are encountering some TypeScript errors within the Admin SDK. This could be due to either not using TypeScript in your client-side JavaScript, or having a stricter compiler on the server side. In any case, resolving each issue is necessary, or transitioning to plain JavaScript in Cloud Functions.

Among the various errors, one of the most commonly seen is:

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

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

You initially define ref as follows:

let ref = firestoreDB.collection('events')

Since collection('events') returns a CollectionReference, the variable ref inherits that type. Therefore, an error occurs when trying to assign ref.where('cafeId', '==', cafe), which is of type

Query</code (an ancestor of <code>CollectionReference
). This results in a type mismatch.

The solution is to immediately declare ref as type

Query</code:</p>
<pre><code>let ref: Query = firestoreDB.collection('events')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for efficiently deconstructing JSON arrays, objects, and nested arrays

I'm attempting to destructure a JSON file with the following structure: [ { "Bags": [ { "id": 1, "name": "Michael Kors Bag", "price": 235, "imgURL" ...

Is there a way to showcase just the month in one spot when presenting data using Angular 13?

Just starting out with Angular and facing a challenge in the Milestone section. There is a loop for displaying years and months, but I need to ensure that the month name is displayed only once for each specific year. Can someone provide me with a possible ...

Retrieving Vue data from parent components in a nested getter/setter context

<template> <div id="app"> {{ foo.bar }} <button @click="meaning++">click</button> <!--not reactive--> <button @click="foo.bar++">click2</button> </div> </templ ...

The React app hosted on Firebase displays a message saying "please activate JavaScript to run this application"

I created a web app using React that is hosted on Firebase Hosting, with a backend server utilizing Express and Cloud Functions. You can access the website here: The landing, login, and signup pages are functioning properly. However, when trying to acces ...

Sendgrid NodeJS has already received the previous email that was sent

My current tech stack includes NextJS, Firebase (Auth, DB, etc...), Vercel for hosting, OVH Domains, and using Node with NextJS for the backend. Recently, I have been experiencing strange behavior when sending emails using Sendgrid library or API v3 direc ...

What is the most graceful method to define a class attribute just once?

Is there a clean and efficient way to set a value only once within a TypeScript class? In other words, is there a way to make a value read-only after it has been assigned? For instance: class FooExample { public fixedValue: string; public setFixe ...

Encountering difficulty importing TypeScript files dynamically within a Deno executable

When attempting to import a file from aws in an exe using its public link based on user input, I am facing difficulties For example, I generated my exe with the command below deno compile --allow-all main.ts Users execute this exe using commands like ./e ...

Retrieving an Observable within an Observable is a feature found in Angular 9

Seeking a solution to return an Observable nested within another Observable. Although I've tried using the pipe and map operators, it doesn't appear to be functioning correctly for me. What could be causing the issue? My development environment ...

Using Angular to include a forward slash "/" in the text input for a date field

Hello everyone, I am a newcomer to AngularJS and I am looking to insert slashes in an input type text element. I prefer not to rely on external packages like angular-ui or input type Date. My goal is to have the format mm/dd/yyyy automatically applied as ...

Determining in Angular 8 whether a value has been altered by a user or by a method call

Within my select element, the value is currently being assigned through an ngOnInit call. Here is an example of the HTML code: <select name="duration" [(ngModel)]="exercisePlan.duration" (ngModelChange)="onChange($event)"> <option *ngFor="l ...

Enroll a nearby variable "Data" to an Observable belonging to a different Component within an Angular application

Looking to update the HTML view using *ngIf, depending on a local variable that should change based on an observable variable from a shared service. HTML <div class="login-container" *ngIf="!isAuthenticated"> TypeScript code for the same componen ...

Angular - Implementing a debounce feature for event handling

Currently, I am utilizing the mouseenter and mouseleave events to control the opening and closing of my sidenav within the app. However, I have encountered issues because when hovering over the container quickly, these events are triggered multiple times ...

Using Jest: A guide to utilizing a mocked class instance

When working on my frontend React application, I decided to use the auth0-js library for authentication purposes. This library provides the WebAuth class which I utilize in my code by creating an instance like so: import { WebAuth } from 'auth0-js&ap ...

Button for enabling and disabling functionality, Delete list in Angular 2

I am looking to toggle between the active and inactive classes on a button element. For example, in this demo, there are 5 buttons and when I click on the first button it removes the last one. How can I remove the clicked button? And how do I implement the ...

The specified 'Object' type does not match the required 'Document' constraint

I need assistance with running a MERN application to check for any issues, but I keep encountering this error across multiple files. Error: The 'CatalogType' type does not meet the requirements of 'Document'. The 'CatalogType&apo ...

The latest release of Angular2, rc1, eliminates all parameters that are not in

In the previous beta version, I was able to analyze using split Location.path(), but now it seems to have been removed. How can I prevent this removal? Interestingly, everything works well with matrix parameters (;id=123;token=asd). This was tested on a ...

The object literal's property 'children' is assumed to have a type of 'any[]' by default

Is there a way to assign the property myOtherKey with any value? I encountered a Typescript error that says.. A problem occurred while initializing an object. The property 'children' in the object literal implicitly has an array type of 'a ...

Experiencing CORS problem in Ionic 3 when accessing API on device

I am a newcomer to IONIC and I am utilizing a slim REST API with Ionic 3. Currently, I am encountering the following error: "Failed to load : Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin&apos ...

Troubleshooting Angular MIME problems with Microsoft Edge

I'm encountering a problem with Angular where after running ng serve and deploying on localhost, the page loads without any issues. However, when I use ng build and deploy remotely, I encounter a MIME error. Failed to load module script: Expected a ...

Leverage Prisma's auto-generated types as the input type for functions

Exploring the capabilities of Prisma ORM has led me to experiment with creating models and generating the PrismaClient. Initially, I thought it would be possible to utilize the generated types for variables and response types, but that doesn't seem to ...