I'm currently working on integrating Angular 18 with Firebase Cloud Functions to interact with a MongoDB database. I have opted to use signals for this purpose, but I keep encountering a type error.
Below is the content of my index.js file:
exports.atlasWordObservable = functions.https.onCall((lemma) => {
const query = { l: lemma };
return mongodb.connect().then(() => {
return collection.find(query).toArray();
});
});
After defining the function in the file above, I attempt to call it as shown below:
import { getFunctions, httpsCallable } from '@angular/fire/functions';
...
entrySingleArray$ = signal<Entry[]>([]);
functions = getFunctions();
atlasLemma = httpsCallable(this.functions, 'atlasWordObservable');
emptyEntry: Entry[] = [{ id: 0, alpha: 0, l: ''}];
getFireLemma(lemma?: string): Observable<Entry[]> {
return (
lemma
? from(this.atlasLemma({name: lemma}))
: of(this.emptyEntry)
).pipe(
tap((response) => {
this.entrySingleArray$.set(response);
console.log(this.entrySingleArray$());
}),
);
}
However, while executing the code, the IDE highlights an error at the return
line and displays the following message:
Type 'Observable<HttpsCallableResult> | Observable<Entry[]>' is not assignable to type 'Observable<Entry[]>'.
As per my understanding, I have two options available: either convert the returned value of the function from
HttpsCallableResult<unknown>
to Entry[]
(I'm unsure how to do this), or modify the function in javascript to return the desired Entry[]
type, which seems only possible if I switch to using TypeScript.
Your assistance in resolving this issue would be greatly appreciated.