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

Minimize the amount of information retrieved and shown based on the timestamp

I am currently working on storing and retrieving the date of a user request. For creating the timestamp, I use this code: const date = firebase.firestore.FieldValue.serverTimestamp(); This is how I fetch and display the data: <tr class="tr-content" ...

Is it possible to configure npm to publish to an organization different from the one automatically detected from package.json?

We are looking to implement a process in our open source project where all Pull Requests will be published to npm using CI/CD. To reduce the potential for supply chain attacks, we aim to deploy to a separate organization. Can this be achieved without makin ...

What could be causing the Firebase email verification to result in a 400 error?

I've been struggling to implement email verification in my React website. Every time I try to use the sendSignInLinkToEmail function, I keep encountering this error: XHRPOSThttps://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=XXXXXXXXXXX ...

Is it possible for the binding model of Mat-Checkbox to be optional or null?

Greetings everyone! I am a newcomer to the world of Angular, where I have successfully created a dynamic list of checkboxes. However, I have encountered a challenge when trying to bind selected values from an API to these checkboxes. <div *ngFor="let b ...

Is there a way to remove the old React component when there are two instances of it still active while passing variables?

Recently, I've encountered some unusual behavior with my React component. As a newcomer to React, I have a page where users can configure toast notifications that are displayed. For this functionality, I'm utilizing the react-hot-toast package. U ...

Retrieve predefined values from a TypeScript controller in Stimulus using the default Symfony configurations

Currently, I am delving into the realm of Stimulus using a standard Symfony6 and Encore setup, with the only notable difference being my use of Typescript. As per the guidance in the Stimulus documentation, typed values should be utilized in the following ...

The AOT Compilation error occurs in Angular2 RC6 when trying to call the function RouterModule.forChild(ROUTES) which is not supported

Operating Environment: Windows 10, IntelliJ 2016.2, node Angular Version: 2.0.0-rc.6 Language: [all | TypeScript X.X | ES6/7 | ES5] Typescript ES6 Node (for Ahead of Time Compilation issues): node --version = Node 4.4.7, NPM 3.10.6 The AOT com ...

Enhancing NG Style in Angular 6 using a custom function

Today, my curiosity lies in the NG Style with Angular 6. Specifically, I am seeking guidance on how to dynamically update [ngStyle] when utilizing a function to determine the value. To better illustrate my query, let me present a simplified scenario: I ha ...

Why does the data appear differently in Angular 9 compared to before?

In this particular scenario, the initial expression {{ bar }} remains static, whereas the subsequent expression {{ "" + bar }} undergoes updates: For example: two 1588950994873 The question arises: why does this differentiation exist? import { Com ...

The 'data-intro' property cannot be bound to the button element as it is not recognized as a valid property

I've been using the intro.js library in Angular 8 and so far everything has been working smoothly. However, I've hit a roadblock on this particular step. I'm struggling to bind a value in the data-intro attribute of this button tag. The text ...

What is the proper way to define the Typescript type of the return value from the dispatch function in a Redux application?

fetchSomething is defined as follows: export const fetchSomething = createAsyncThunk( 'something', async () => { return Promise.resolve({name: 'jack'}) } ) This is the code within a React component: const dispatch = useApp ...

When utilizing custom ngDoBootstrap and createCustomElement in Angular, the router fails to recognize the URL being used

WHEN I implement a custom ngDoBootstrap function instead of the default bootstrap: [AppComponent] like shown below: @NgModule({ imports: [ BrowserModule, FormsModule, AppRoutingModule ], declarations: [ AppComponent, HelloComponent ], exports: ...

The http post request is functioning properly in postman, but it is not working within the ionic app

I am currently developing an app in ionic v3 within visual studio (Tools for Apache Cordova). On one of the screens in my app, I gather user information and send it to an API. However, I'm encountering an issue with the HTTP POST request that I'm ...

What are the benefits of utilizing TypeScript declarations? How can you demonstrate their value with concrete examples?

I'm a bit confused about the use of declaration in TypeScript. It seems like the compiler doesn't compile it into the js file, so what is the purpose and advantage of using declaration? Can someone please explain this to me? ...

What are the steps to incorporating the pick function in TypeScript?

The TypeScript documentation mentions a pick function that is declared but not implemented. In an attempt to create a simplified version, I wrote the following: function pick<T, K extends keyof T>(obj: T, key: K): Pick<T, K> { return { [key]: ...

Overcoming Duplicate Messages and Enhancing Performance in Chat Functionality with useOptimistic Method

I'm currently implementing a chat functionality in my Next.js application with Firebase Firestore, and I have encountered two main issues: // UpdatedChatWrapper.tsx const UpdatedChatWrapper = ({ user, allMessages, sendMessageAction }: Props) => { ...

How can I extract a value from an object that is readonly, using a formatted string as the key?

I encountered a situation where I have code resembling the following snippet. It involves an object called errorMessages and multiple fields. Each field corresponds to various error messages in the errorMessages object, but using a formatted string to retr ...

The error message "TypeError: router.back is not a function" indicates that

Testing out the back route navigation in next.js and encountering an error during the test: The function for going back: const router = useRouter(); const handleClick = useCallback(() => { if (router) { router.back(); ...

ConfirmUsername is immutable | TypeScript paired with Jest and Enzyme

Currently, I am experimenting with Jest and Enzyme on my React-TS project to test a small utility function. While working on a JS file within the project, I encountered the following error: "validateUsername" is read-only. Here is the code for the utilit ...

Sort through the files for translation by utilizing a feature within Typewriter

I am looking to implement Typewriter in a project that involves translating many C# files into TypeScript using WebEssentials. Is there a way to configure the template so that only class files containing a specific attribute are translated in this mann ...