android - 更新 Firestore 中的单个文档字段
问题描述
我有一个文档参考user/userid23435534
,我想更新该文档中的单个(文档有多个字段)字段(昵称)。
我称这种方法为:ref.ref.update("nick","test123")
我可以从日志中看到ref.getpath()
确实user/userid23435534
如此。
但是,在调用此方法并从我那里获得成功后,OnSuccessListener
我仍然看到我的字段未在 firestore 数据库中更新。我在这里做错了什么?
编辑:
public static void updateDocument(){
final DocumentReference ref = db.collection("user").document("userid23435534");
ref.update("nick", "test123" ) //logging shows that red.getPath is "user/userid23435534"
.addOnSuccessListener(aVoid -> {
//success is called when calling method that runs this code
}).addOnFailureListener(e -> {
//....
})
}
解决方案
我刚刚在我的本地模拟器中运行了这段代码,它更新了文档没有问题:
DocumentReference ref = db.collection("56246892").document("uid");
ref.update("nick", "test123" ).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
System.out.println("Updated");
}
});
您确定该文档已经存在吗?这是update()
工作所必需的。如果您不确定文档是否存在,请使用set(..., SetOptions.merge())
.
推荐阅读
- javascript - HTML import is not working in Electron app
- laravel - useradd invalid user in Dockerfile
- python - Is there anyway to solve this without using two for loop
- python - how do i create a list from csv contents and then search within that list
- python-3.x - Using the Python math.prod() in a specified Pandas date range
- gradient - Why is Policy Gradient method for Reinforcement Learning good for LARGE action spaces
- microservices - Batch operation over thousands of aggregates in CQRS system, do people do that?
- python - 如何在python中创建产品订单的共现矩阵?
- c - How to initialize a data structure (eg. linked list) with functions in C?
- ios - 有没有像@ObservedObject 这样不涉及创建新类的东西?