onSnapshot出错:FirebaseError:[code = invalid-argument]:事务已关闭

时间:2017-10-22 17:41:14

标签: google-cloud-firestore

我以递归方式加载存储在Firestore中的树。树有~79个节点。

偶尔我会收到此错误(大约十分之一的全树负载)。

修改:代码:https://github.com/karol-depka/OrYoL

编辑:部署的示例:https://oryol.karoldepka.com/tree(对不起,现在没有吸毒者,只是这个)

详情如下。

package.json中的Firebase版本:4.5.0

问题:

我在哪里可以获得比此基本文档更多的信息https://firebase.google.com/docs/reference/js/firebase.FirebaseError

问题的根源是什么以及如何解决?

3VM724:27 Uncaught Error in onSnapshot: Error: transaction closed
at new FirestoreError (error.js:164)
at JsonProtoSerializer.webpackJsonp.../../../../firebase/firestore/remote/serializer.js.JsonProtoSerializer.fromRpcStatus (serializer.js:126)
at JsonProtoSerializer.webpackJsonp.../../../../firebase/firestore/remote/serializer.js.JsonProtoSerializer.fromWatchChange (serializer.js:517)
at PersistentListenStream.webpackJsonp.../../../../firebase/firestore/remote/persistent_stream.js.PersistentListenStream.onMessage (persistent_stream.js:334)
at persistent_stream.js:270
at persistent_stream.js:247
at async_queue.js:81
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:392)
at Object.onInvoke (core.es5.js:3890)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)

代码:

private processNodeEvents(nestLevel: number, snapshot: any, parents, listener: DbTreeListener) {
  const serviceThis = this
  snapshot.docChanges.forEach(function(change) {
    let data = change.doc.data()
    if (change.type === 'added') {
      const parentsPath = serviceThis.nodesPath(parents)
      console.log('node: ', nestLevel, parentsPath, data);
      serviceThis.pendingListeners ++
      data.node.onSnapshot(targetNodeDoc => {
        serviceThis.pendingListeners --
        listener.onNodeAdded(
          new NodeAddEvent(parentsPath, parentsPath[parentsPath.length - 1], targetNodeDoc, targetNodeDoc.id,
            serviceThis.pendingListeners))
        console.log('target node:', nestLevel, targetNodeDoc)
        console.log('target node title:', nestLevel, targetNodeDoc.data().title)

        const subCollection = targetNodeDoc.ref.collection('subNodes')
        console.log('subColl:', subCollection)
        subCollection.onSnapshot((subSnap: QuerySnapshot) => {
          const newParents = parents.slice(0)
          newParents.push(targetNodeDoc.ref)
          serviceThis.processNodeEvents(nestLevel + 1, subSnap, newParents, listener)
        })
      })
      // console.log('root node ref: ', targetNode);
    }
    if (change.type === 'modified') {
      console.log('Modified city: ', data);
    }
    if (change.type === 'removed') {
      console.log('Removed city: ', data);
    }
  })
}

编辑:通过多次运行代码发现了另一个错误:

VM3343:27 Uncaught Error in onSnapshot: Error: The referenced transaction has expired or is no longer valid.
at new FirestoreError (error.js:164)
at JsonProtoSerializer.webpackJsonp.../../../../firebase/firestore/remote/serializer.js.JsonProtoSerializer.fromRpcStatus (serializer.js:126)
at JsonProtoSerializer.webpackJsonp.../../../../firebase/firestore/remote/serializer.js.JsonProtoSerializer.fromWatchChange (serializer.js:517)
at PersistentListenStream.webpackJsonp.../../../../firebase/firestore/remote/persistent_stream.js.PersistentListenStream.onMessage (persistent_stream.js:334)
at persistent_stream.js:270
at persistent_stream.js:247
at async_queue.js:81
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:392)
at Object.onInvoke (core.es5.js:3890)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)
window.console.error @ VM3343:27

编辑:将firebase更新为4.6.0,问题发生了3次:

Uncaught Error in onSnapshot: Error: transaction closed
  at new FirestoreError (error.js:149)
  at JsonProtoSerializer.webpackJsonp.../../../../@firebase/firestore/dist/esm/src/remote/serializer.js.JsonProtoSerializer.fromRpcStatus (serializer.js:93)
  at JsonProtoSerializer.webpackJsonp.../../../../@firebase/firestore/dist/esm/src/remote/serializer.js.JsonProtoSerializer.fromWatchChange (serializer.js:536)
  at PersistentListenStream.webpackJsonp.../../../../@firebase/firestore/dist/esm/src/remote/persistent_stream.js.PersistentListenStream.onMessage (persistent_stream.js:309)
  at persistent_stream.js:246
  at persistent_stream.js:222
  at async_queue.js:62
  at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:392)
  at Object.onInvoke (core.es5.js:3890)
  at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)

编辑:即使发生错误,树似乎仍在继续加载。

修改:错误的另一个版本,code=aborted(将firebase升级为4.6.0后):

Error in onSnapshot: FirebaseError: [code=aborted]: The referenced transaction has expired or is no longer valid.
/vendor.bundle.js:18588 errHandler()
/vendor.bundle.js:33367
/polyfills.bundle.js:2970 ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask()
/vendor.bundle.js:107276 Object.onInvokeTask()
/polyfills.bundle.js:2969 ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask()
/polyfills.bundle.js:2737 Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask()
/polyfills.bundle.js:3044 webpackJsonp.../../../../zone.js/dist/zone.js.ZoneTask.invokeTask()
/polyfills.bundle.js:3033 ZoneTask.invoke()

1 个答案:

答案 0 :(得分:1)

我或多或少都是一样的,当它起作用时它是随机的而且不是。我不使用快照,而是使用valueChanges

ERROR Error: transaction closed
at new FirestoreError (error.js:149)
at JsonProtoSerializer.webpackJsonp.../../../../@firebase/firestore/dist/esm/src/remote/serializer.js.JsonProtoSerializer.fromRpcStatus (serializer.js:93)
at JsonProtoSerializer.webpackJsonp.../../../../@firebase/firestore/dist/esm/src/remote/serializer.js.JsonProtoSerializer.fromWatchChange (serializer.js:536)
at PersistentListenStream.webpackJsonp.../../../../@firebase/firestore/dist/esm/src/remote/persistent_stream.js.PersistentListenStream.onMessage (persistent_stream.js:309)
at persistent_stream.js:246
at persistent_stream.js:222
at async_queue.js:62
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:392)
at Object.onInvoke (core.es5.js:3890)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)

对我而言,这种代码的和平会引发错误:

return Observable.forkJoin(entries.map(entry => {
  return this.getPick(entry)
}))

条目中的每个条目用于从firestore返回单个选择。

  private getPick(entryId: number) {
     return this.afs.collection<Pick>('entry/' + entryId + '/event/' + '9/' + 'picks', ref => ref.where('is_captain','==',true))
     .valueChanges()

我不是故意劫持你的线索,但我觉得这非常重要。如果我将forkJoin更改为

Observable.forkJoin(entries.slice(0,20)...

然后它可以工作,所以我猜它是某种超载的查询。