如何知道Firebase实时数据库事务中的实际当前值是否为空?

时间:2018-02-25 18:59:18

标签: firebase firebase-realtime-database transactions es6-promise

firebase事务处理程序应该能够处理空值,并且可以多次调用它。如果在具有空值的位置上调用它会发生什么。 (如果没有现有数据)是否有办法从继续承诺中了解它。下面的代码怎么样?

let isNull = false;
db.ref('/entry').transaction( currentValue => {
  if(!currentValue) {
    isNull = true;
    // Do the updates
  } else {
    // Do the update
  }
  return currentValue;
}).then(() => { 
  console.log("IsNull", isNull);
})

1 个答案:

答案 0 :(得分:0)

此示例用于检查特定用户ID是否在节点

使用以下内容:

//Add this before OnCreate while declaring all variables
FirebaseUser firebaseUserId = FirebaseAuth.getInstance().getCurrentUser();

yourDatabaseSource.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final String thisFirebaseUser = firebaseUserId.getUid();
                if (dataSnapshot.hasChild(thisFirebaseUser)) {

                   //check your condition

                } else {

                   //do something
                }
            }

希望这会有所帮助。

相关问题