Data in transaction is null

后端 未结 1 1363
别那么骄傲
别那么骄傲 2020-11-30 14:05

I have a problem with transactions. The data in the transaction is always null and the update handler is called only singe once. The documentation says :

相关标签:
1条回答
  • 2020-11-30 14:59

    By returning undefined in your if( ... === null ) block, you are aborting the transaction. Thus it never sends an attempt to the server, never realizes the locally cached value is not the same as remote, and never retries with the updated value (the actual value from the server).

    This is confirmed by the fact that committed is false and the error is null in your success function, which occurs if the transaction is aborted.

    Transactions work as follows:

    • pass the locally cached value into the processing function, if you have never fetched this data from the server, then the locally cached value is null (the most likely remote value for that path)
    • get the return value from the processing function, if that value is undefined abort the transaction, otherwise, create a hash of the current value (null) and pass that and the new value (returned by processing function) to the server
    • if the local hash matches the server's current hash, the change is applied and the server returns a success result
    • if the server transaction is not applied, server returns the new value, client then calls the processing function again with the updated value from the server until successful
    • when ultimately successful, and unrecoverable error occurs, or the transaction is aborted (by returning undefined from the processing function) then the success method is called with the results.

    So to make this work, obviously you can't abort the transaction on the first returned value.

    One workaround to accomplish the same result--although it is coupled and not as performant or appropriate as just using the transactions as designed--would be to wrap the transaction in a once('value', ...) callback, which would ensure it's cached locally before running the transaction.

    0 讨论(0)
提交回复
热议问题