【问题标题】:Firebase Firestore transaction is returning a querySnapshot instead of a documentSnapshotFirebase Firestore 事务返回的是 querySnapshot 而不是 documentSnapshot
【发布时间】:2018-08-26 02:09:28
【问题描述】:

我正在从谷歌云函数中的 firebase 文档中复制此代码:

var cityRef = db.collection('cities').doc('SF');
var transaction = db.runTransaction(t => {
  return t.get(cityRef)
  .then(doc => {
    // Add one person to the city population
    var newPopulation = doc.data().population + 1;
    t.update(cityRef, { population: newPopulation });
  });
}).then(result => {
  console.log('Transaction success!');
}).catch(err => {
  console.log('Transaction failure:', err);
});

但我得到:property 'data' does not exist on type 'QuerySnapshot' 它应该是一个文档快照。

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    我在 github 上打开一个 issue 并获得一些见解后找到了答案 https://github.com/firebase/firebase-admin-node/issues/344

    async function setCounts(storeRef: admin.firestore.DocumentReference) { // Type here
    
      var transaction = admin.firestore().runTransaction(t => {
        return t.get(storeRef)
        .then((doc: admin.firestore.DocumentSnapshot) => { // Type here
          x = doc.data(); // Now i can get data() from the doc
        });
      }).then(result => {
        console.log('Transaction success!');
      }).catch(error => {
        console.log('Transaction failure:', error);
      });
    
    }
    

    我最终通过显式声明 DocumentReference 和 DocumentSnapshot 的类型使其工作,我不知道为什么,但是在部署 linter 推断文档作为 QuerySnapshot 时,即使它不是。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2021-01-20
      • 2019-07-06
      • 2018-12-09
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多