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

时间:2017-10-23 14:56:18

标签: 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'});
  });
});

我的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
} 

是否可以将firebase.firestore.batch()与云功能一起使用?

谢谢

1 个答案:

答案 0 :(得分:3)

()之后您遗漏了admin.firestore

改变这个:

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

到此:

var batch = admin.firestore().batch();