【问题标题】:Unreachable code detected after transaction交易后检测到无法访问的代码
【发布时间】:2018-02-18 20:49:44
【问题描述】:

我在打字稿文件中收到警告“检测到无法访问的代码”。运行 Firebase 事务后没有任何效果。这是交易代码:

// Create Firestore reference
let pointsRef = 'Users/'+this.user.uid;
var pointsDocRef = this.afs.doc(pointsRef).ref;

return this.afs.firestore.runTransaction((transaction) => {
  return transaction.get(pointsDocRef).then((ptsDoc) => {
    if(!ptsDoc.exists){
      throw "Document does not exist!"
    }

    var newPtsScore = ptsDoc.data().points - 20;
    transaction.update(pointsDocRef, { points: newPtsScore });             
  });
}).then(() => {
  console.log('Point count successfully decremented for new item'); 
  // Close dialog
  this.dialog.closeAll();
}).catch(function(error) {console.log('Transaction failed: ', error);});

console.log('Hey there!'); <-- "UNREACHABLE CODE DETECTED"

【问题讨论】:

  • 你在上面有一个回报:return this.afs....

标签: javascript angular firebase google-cloud-firestore unreachable-code


【解决方案1】:

您的日志行紧跟在return 语句之后。它永远不会运行,因为return 离开了函数。

【讨论】:

  • 哪个返回语句?
  • 与日志行在同一列的那个。第一个。另一个函数中的第二个。
  • @FiringBlanks return this.afs.firestore 5])... 全部返回,然后您在函数返回后尝试打印。
  • 这就是根据文档完成交易的方式。这是否意味着每个文件只能有一个事务,后面没有代码?
  • @FiringBlanks 一定要返回runTransaction的结果吗?
猜你喜欢
  • 2011-05-09
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
相关资源
最近更新 更多