【问题标题】:Firebase JS Error: getToken aborted due to token changeFirebase JS 错误:getToken 由于令牌更改而中止
【发布时间】:2018-09-26 08:53:24
【问题描述】:

我在使用 JavaScript 库运行 Firestore 事务时收到 Firebase 错误“错误:getToken aborted due to token change”。错误不会每次都抛出,我找不到模式。我想我已经在某处实现了一些竞争条件。

我的应用中的用户流程是这样的:

  1. 注册一个新帐户并以相同的形式提交一个额外的字符串
  2. 注册后使用相同的凭据登录用户
  3. 登录后,获取该附加字符串并将其保存到 Firestore(在事务中)。
  4. 由于Error: getToken aborted due to token change.,事务失败

承诺流程:

    firebase.auth().createUserWithEmailAndPassword(email, password)
      .catch(signupError => {
        // no problems here
      })
      .then(() => {
        return firebase.auth().signInWithEmailAndPassword(email, password)
      })
      .catch(loginError => {
        // no problem here
      })
      .then((user) => {
        // Database write call which fails (see lower code block)
        return this.claimInput.writeClaimedPlace(user.user.uid, claimedPlace);
      })
      .catch(error => {
        // "getToken aborted" ERROR GETS CAUGHT HERE, transaction fails    
      })    
    }

数据库事务调用

firestore.runTransaction(function(transaction) {

  return transaction.get(usersBusinessRef).then(function(usersBusinesDoc) {

    let claimedPlaces = [];
    if (usersBusinesDoc.exists && usersBusinesDoc.data().claimedPlaces) {
      claimedPlaces = usersBusinesDoc.data().claimedPlaces;
    }
    claimedPlaces.push(claimedPlace);

    return transaction.set(usersBusinessRef, { claimedPlaces }, { merge: true });
  });
});

我在谷歌的任何地方都找不到错误。

我认为该错误是由登录时发生的令牌更改引起的。另一方面,我读到 Firebase 会再接受旧令牌。有什么想法吗?

【问题讨论】:

  • 这也发生在我身上(android)。我检查了已经登录的当前用户,但是当我运行事务时,由于类似的错误而失败。当前的解决方法是手动获取数据然后更新。

标签: firebase firebase-authentication google-cloud-firestore


【解决方案1】:

我在调试通过 React 应用程序连接到 firebase 的客户端时遇到了类似的错误[错误代码]。

解决办法是

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write: if false;
    }
  }
}

将上述内容放在 firestore 设置的规则部分中,这显然意味着您需要允许读取外部 api,但写入被阻止,并且以前阻止读取和写入。

如果您尝试从客户端/服务器读取数据,这可能是问题之一

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write;
    }
  }
}
  • 免责声明: * 我不是 firebase 的专家。我不确定这是否会损害您的数据库以由外部 api 编写,因为您正在通过这样做打开 Firestore 防火墙

附:有一个firebase-admin 模块,我认为它有助于通过以单独的方式处理身份验证来进行写入。我认为该模块更适合写入,而普通的firebase.firestore(app) 适合读取。

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多