在Cloud Functions for Firebase中运行Firestore批处理

zel*_*g74 2 node.js firebase google-cloud-functions google-cloud-firestore

在Firebase的Cloud Functions中,我尝试运行admin.firestore.batch(),但收到错误“ admin.firestore.batch不是函数”,请参阅(完全简化)示例。

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'});
  });
});
Run Code Online (Sandbox Code Playgroud)

我的package.json是

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "cors": "^2.8.1",
    "firebase-admin": "^5.4.2",
    "firebase-functions": "^0.7.1"
  },
  "private": true
} 
Run Code Online (Sandbox Code Playgroud)

是否可以在Cloud Functions中使用firebase.firestore.batch()?

谢谢

Bob*_*der 8

你错过了()之后admin.firestore

更改此:

var batch = admin.firestore.batch(); // <--- ERROR HERE
Run Code Online (Sandbox Code Playgroud)

对此:

var batch = admin.firestore().batch();
Run Code Online (Sandbox Code Playgroud)