javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functions

"TypeError: functions.firestore.document is not a function in Firebase Cloud Functions"


I haven't changed my firebase functions in a while, yesterday I wanted to create a new firebase function and added it to the top of all my functions, so this are my first lines:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const uuid = require('uuid');

admin.initializeApp();
const db = admin.firestore();
const storage = admin.storage().bucket();
const app = express();
app.use(cors({ origin: true }));

exports.runPredictionOnUpload = functions.firestore
.document('Project/{projectID}/models/{modelID}/predictions/{docId}')
.onCreate(async (snap, context) => {
const { projectID, modelID, docId } = context.params;
const document = snap.data();

if (!document || !document.data) {
  console.error('Missing data field in the document.');
  await admin.firestore().collection('Project').doc(projectID).collection('models').doc(modelID).collection('predictions').doc(docId).update({
    status: 'error',
    error: 'Data field is missing',
  });
  return;
}

}

The function is not done, yet. But if I now run firebase deploy --only functions, I get the following error:

TypeError: functions.firestore.document is not a function

The problem is that my next cloud functions (which I haven't changed in months and always used to work) also starts with functions.firestore.document, and so If i remove the new function I get the same error with all of my "old" functions. So now I cannot push updates out anymore or add new functions. Does anyone know what goes wrong?


Solution

  • It looks like you upgraded your firebase-functions dependency without also updating your code. Your functions are written with the old 1st-gen API, but the latest firebase-functions uses the new 2nd-gen API. You should probably start migrating your code to the new API. Either that, or keep using an old version of firebase-functions without upgrading it.