在尝试更新值和编写文档之前,我试图在Firestore事务中检索文档,但是无论何时启动事务,我都会不断从快照中获取空值。但是,当我尝试获取文档ID时,实际上我确实拥有它。
基本上,我想检索在服务中心子集合中创建的最后一个计数器(按编号),该计数器被另存为服务中心文档本身中的属性,并使用新编号创建一个计数器,同时还要更新“ lastCounter”属性在服务中心。
我将“ lastCounter”检索为双精度,我尝试使用long,但它不起作用。我也无法获得其他任何属性。
DocumentReference scReference = db
.collection("Services")
.document(uuid);
Log.i("AAA", "STARTED TRANSACTION");
// get the service centre
DocumentSnapshot serviceCenterSnapshot = transaction.get(scReference);
Log.i("AAA", serviceCenterSnapshot.getId()); // this one retireves the id which shows that it's working
Log.i("AAA", "last counter = " + serviceCenterSnapshot.getDouble("lastCounter")); // null
Log.i("AAA", "branch = " + serviceCenterSnapshot.getString("branch")); // null
```
// get the last counter added and increment it
double lastCounterNumber = serviceCenterSnapshot.getDouble("lastCounter");
double newCounterNumber = lastCounterNumber + 1;
Counter counter = new Counter(newCounterNumber);
// update the service centre with the last counter added
transaction.update(scReference, "lastCounter", newCounterNumber);
// add the new counter
transaction.set(scReference.collection("Counters").document(), counter);
return null;
日志
2019-04-01 18:05:32.966 29597-29656/com.zayed.qmsadmin I/AAA: STARTED TRANSACTION
2019-04-01 18:05:33.405 29597-29656/com.zayed.qmsadmin I/AAA: rkO0i0sQHuWFWqNUb8J3XE1QV6E3
2019-04-01 18:05:33.405 29597-29656/com.zayed.qmsadmin I/AAA: last counter = null
2019-04-01 18:05:33.405 29597-29656/com.zayed.qmsadmin I/AAA: branch = null
2019-04-01 18:05:33.406 29597-29597/com.zayed.qmsadmin I/AAA: FINISHED TRANSACTION
2019-04-01 18:05:33.406 29597-29597/com.zayed.qmsadmin I/AAA: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference