我在创建用户后写入 Firestore,代码如下:
userMap.put("email", user.getEmail());
userMap.put("display_name", user.getDisplayName());
userMap.put("user_id", user.getUid());
userMap.put("provider", user.getProviders());
mStoreBaseRef.collection(USERS).add(userMap);
将此用户写入 Firestore 时,会为该用户生成一个唯一 ID。
稍后,我想写入用户节点,但我没有生成的唯一 key 。我根据用户的特定 ID 查询“用户”节点,以便我可以写入该节点,但我不确定如何获取该特定用户的 key :
Query x = mStoreBaseRef.collection(USERS_LABEL).whereEqualTo("user_id", mPollCreatorID);
x.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot d : task.getResult()){
User user = d.toObject(User.class);
Log.v("USER", user.getUser_id());
//I need to add the document here of the unique key
mStoreBaseRef.collection(USERS_LABEL).add(followersMap);
}
}
我正在尝试从下面获取 mAQGM9S.......
最佳答案
文档的 ID 可通过 DocumentSnapshot.getId()
获得.所以:
Query x = mStoreBaseRef.collection(USERS_LABEL).whereEqualTo("user_id", mPollCreatorID);
x.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot d : task.getResult()){
Log.v("ID", d.getId());
User user = d.toObject(User.class);
Log.v("USER", user.getUser_id());
//I need to add the document here of the unique key
mStoreBaseRef.collection(USERS_LABEL).add(followersMap);
}
}
关于java - 无法访问 Firestore 节点中的文档 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53453224/