我正在向firestore发布一些数据,我正在尝试生成一个与文档ID相同的帖子ID,但它没有给我一个正确的值

问题描述 投票:0回答:1

我,我发送数据到firestore,我想为我发送的每个帖子附上post_id作为post_id ...我正在使用android

final String randomName= FieldValue.serverTimestamp().toString();
// final String postId2=firebaseFirestore.collection("posts").document().getId();
final String postId2=firebaseFirestore.collection("posts").getParent().getId();
StorageReference filepath=storageReference.child("houses_desc").child(randomName+ ".jpg");
filepath.putFile(postImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
        if (task.isSuccessful()){
            String downloadUri=task.getResult().getDownloadUrl().toString();
            Map<String,Object> houseMap=new HashMap<>();
            houseMap.put("user_id",current_user_id);
            houseMap.put("image1",downloadUri);
           // houseMap.put("image2",mPostImage2);
            houseMap.put("house_type",title);
            houseMap.put("desc",description);
            houseMap.put("price",price);
            houseMap.put("province",stateProvince);
            houseMap.put("city",city);
            houseMap.put("email",email);
            houseMap.put("post_id",postId2);
            houseMap.put("time_stamp", FieldValue.serverTimestamp());
java android google-cloud-firestore
1个回答
0
投票

我之前已经为JavaScript回答了这个问题。见Save id to Firestore document

Android SDK implementation of CollectionReference.document()中,新文档的ID通过调用:

Util.autoId()

如果你看看source for Util.autoId它是一个简单的随机ID生成器。您可以从代码中调用Firestore的Util.autoId(),也可以自己实现类似的功能。

final String postId2 = Util.autoId();
© www.soinside.com 2019 - 2024. All rights reserved.