admin管理员组文章数量:1427458
I have a simple transaction which should return the count of a child node stored in my realtime database and increment it. It might be worth noting this transaction is inside of a firebase cloud onWrite function.
The issue is that the data returned is null. I know this is possible when the client data is stale however the function exits without error and mitted is false.
When I use the exact same reference to retrieve a data snapshot, the correct value is returned.
var ref = admin.database(devDB).ref('/path/to/countValue');
ref.transaction(function(count) {
if(count != null) {
var newCount = count + 1;
return newCount;
}
}, function(error, mitted, ss) {
if(error) {
console.log('error: ', error);
}
console.log('mitted: ', mitted);
});
I have a simple transaction which should return the count of a child node stored in my realtime database and increment it. It might be worth noting this transaction is inside of a firebase cloud onWrite function.
The issue is that the data returned is null. I know this is possible when the client data is stale however the function exits without error and mitted is false.
When I use the exact same reference to retrieve a data snapshot, the correct value is returned.
var ref = admin.database(devDB).ref('/path/to/countValue');
ref.transaction(function(count) {
if(count != null) {
var newCount = count + 1;
return newCount;
}
}, function(error, mitted, ss) {
if(error) {
console.log('error: ', error);
}
console.log('mitted: ', mitted);
});
Share
Improve this question
edited May 1, 2018 at 22:46
Doug Stevenson
319k36 gold badges456 silver badges473 bronze badges
asked May 1, 2018 at 22:15
PadawanPadawan
8201 gold badge10 silver badges19 bronze badges
1 Answer
Reset to default 6You can expect that the first invocation of a transaction handler will hand you a null value. This is stated in the documentation:
Transaction Function is Called Multiple Times
Your transaction handler is called multiple times and must be able to handle null data. Even if there is existing data in your database it may not be locally cached when the transaction function is run.
The problem is that your function doesn't return a value in the case where the value is null. This signals to the transaction that you want to abort the transaction. According to the API docs for transaction():
The function should return the new value it would like written (as a JavaScript object). If undefined is returned (i.e. you return with no arguments) the transaction will be aborted and the data at this location will not be modified.
The sample code in the documentation I linked earlier that performs an increment always returns a value:
upvotesRef.transaction(function (current_value) {
return (current_value || 0) + 1;
});
本文标签: javascriptFirebase transaction returns null and completes without errorStack Overflow
版权声明:本文标题:javascript - Firebase transaction returns null and completes without error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745502995a2661116.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论