Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Firestore batch in Cloud Functions for Firebase

In Cloud Functions for Firebase, I am trying to run admin.firestore.batch(), but I am receiving error "admin.firestore.batch is not a function", see (totally simplified) example.

exports.getTest = functions.https.onRequest((request, response) => {
  cors(request, response, () => {
    var batch = admin.firestore.batch(); // <--- ERROR HERE
    var docRef = admin.firestore().doc('tariffs/1')
    batch.set(docRef, 'name', 'free');
    return batch.commit();
  }).then(() => {
    response.status(200).send({result: 'success'});
  });
});

My package.json is

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "cors": "^2.8.1",
    "firebase-admin": "^5.4.2",
    "firebase-functions": "^0.7.1"
  },
  "private": true
} 

Is it possible to use firebase.firestore.batch() with Cloud Functions?

Thank you

like image 317
zelig74 Avatar asked Dec 04 '22 21:12

zelig74


1 Answers

You are missing the () after admin.firestore.

Change this:

var batch = admin.firestore.batch(); // <--- ERROR HERE

to this:

var batch = admin.firestore().batch();
like image 103
Bob Snyder Avatar answered Jan 05 '23 14:01

Bob Snyder