fobo66 / algolia-firebase-functions

Firebase Cloud Functions for sync Firebase Database or Firestore with Algolia in real time
Apache License 2.0
32 stars 9 forks source link

TypeError: Cannot read property 'exists' of undefined at Object.syncAlgoliaWithFirestore #296

Closed miag676 closed 3 years ago

miag676 commented 3 years ago

I'm trying to use Algolia with Firebase Functions, but I get this error TypeError: Cannot read property 'exists' of undefined at Object.syncAlgoliaWithFirebase (/workspace/node_modules/algolia-firebase-functions/dist/index.js:79:23) I followed the steps on the npm algolia-firebase-functions library (https://www.npmjs.com/package/algolia-firebase-functions).

How could I get rid of this error? Any help would be appreciated!

This is my Firebase function code:


const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
const db = admin.firestore()
const algoliasearch = require("algoliasearch");
const algoliaFunctions = require('algolia-firebase-functions');

const algolia = algoliasearch(functions.config().algolia.app,
                              functions.config().algolia.key);
const index = algolia.initIndex(functions.config().algolia.index);

exports.indexBesedilo = functions.firestore.document('besedila/{document}').onCreate((change, context) =>
    algoliaFunctions.syncAlgoliaWithFirebase(index, change)
)```
fobo66 commented 3 years ago

Thanks for reporting this! I'm looking into it.

fobo66 commented 3 years ago

Turns out now it only supports ES6 modules for some reason, but I may be mistaken.

As a first thing I've noticed, please use algoliaFunctions.syncAlgoliaWithFirestore(index, change) to sync with Firestore. Types of RealtimeDatabase and Firestore changes are different, thus syncAlgoliaWithFirebase won't work with Firestore.

If it doesn't help, I would suggest converting your functions to the ES6 module and rewrite code like this:

import { config, firestore } from "firebase-functions";
import admin from 'firebase-admin';
admin.initializeApp(config().firebase)
import algoliasearch from "algoliasearch";
import { syncAlgoliaWithFirestore } from 'algolia-firebase-functions';

const algolia = algoliasearch(functions.config().algolia.app,
                              functions.config().algolia.key);
const index = algolia.initIndex(functions.config().algolia.index);

export const indexTest = firestore.document('test/{document}').onCreate((change, context) =>
    syncAlgoliaWithFirestore(index, change)
)

Or use Typescript in your cloud functions.

Please let me know if it works for you

miag676 commented 3 years ago

Hello, thank you very much for your help. I converted the functions to ES6 as you suggested and it works now. Previously, I also tried it with firestore in the command( algoliaFunctions.syncAlgoliaWithFirestore(index, change)), instead of firebase (as the code I posted), but it didn't work. But changing it to ES6 solves the problem. Thanks again!

fobo66 commented 3 years ago

Cool, thank you! I'll update README to make it clear that the library is usable in ES6 modules only