gpt4 book ai didi

java - 带有设置数据的 Firestore 自定义文档 ID 不起作用 - CollectionReference

转载 作者:行者123 更新时间:2023-11-29 23:31:28 25 4
gpt4 key购买 nike

我想在我的 firestore 云中存储我的用户数据。所以文档 ID 应该是来自 auth 的用户 ID。当我手动设置文档 ID 时,我无法设置数据。那是我的代码:

currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();

mFirestore = FirebaseFirestore.getInstance();

CollectionReference mFirestoreProfiles = mFirestore.collection("profiles").document(currentuser).set();

Profile profile = new Profile(
email,
birth
);

mFirestoreProfiles.add(profile)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Snackbar snackbar = Snackbar
.make((getWindow().getDecorView().getRootView()), "Registration successful.", Snackbar.LENGTH_LONG);
snackbar.show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar snackbar = Snackbar
.make((getWindow().getDecorView().getRootView()), e.getMessage(), Snackbar.LENGTH_LONG);
snackbar.show();
}
});

我总是在这一行中遇到“CollectionReference”错误:

mFirestore.collection("profiles").document(currentuser).set();

如果有人愿意花时间帮助我解决这个(非常愚蠢的)问题,那就太好了。

问候杰雷

最佳答案

下面一行代码:

CollectionReference mFirestoreProfiles = mFirestore.collection("profiles").document(currentuser).set();

总是会产生异常。要解决此问题,请将上面的代码行更改为:

DocumentReference mFirestoreProfiles = mFirestore.collection("profiles").document(currentuser);
Profile profile = new Profile(email, birth);
mFirestoreProfiles.set(profile).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "Document written successfully!", Toast.LENGTH_SHORT).show();
}
});

即使您将 profile 对象传递给 set() 方法,也会抛出异常,因为 set() 方法返回类型是 Task 而不是 CollectionReference

关于java - 带有设置数据的 Firestore 自定义文档 ID 不起作用 - CollectionReference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52596668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com