首页 > 解决方案 > 更新 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 -> {
            //....
        })
 }

标签: androidfirebasegoogle-cloud-firestore

解决方案


我刚刚在我的本地模拟器中运行了这段代码,它更新了文档没有问题:

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()).


推荐阅读