0

SwiftUIとFirebaseの初学者です。

Firestoreでは以下のように .document() でドキュメントIDを自動設定してくれますが、その取得がうまく行きません。
取得自体はできていますが、リスト表示にしているためか、最新のものだけに上書きされてしまいます。

自動で設定したドキュメントIDを持ってきてそのIDを指定し、選択したリストを削除したいです。

db.collection("posts").document().setData([
                            "post" : postText,
                            "date" : Date(),
                            "user" : Auth.auth().currentUser!.uid,
                            "id": db.collection("posts").document().documentID
                        ])

下記は関係する部分のコードです。よろしくお願いします。

func gepo() {
    let userID = Auth.auth().currentUser!.uid
    db.collection("posts").whereField("user", isEqualTo: userID)
        .getDocuments(){(QuerySnapshot, err) in
            if let err = err{
                print("Error getting documents: \(err)")
            }else{
                for document in QuerySnapshot!.documents{
                    post = document.get("post") as! String
                  //  date = document.get("date") as! Timestamp
                    postLists.append(post)
                    documentId = document.documentID
                    docIdList.append(documentId)
                }
            }
        }
}

var body: some View{
    let newID = db.collection("posts").document().documentID
    let userID = Auth.auth().currentUser!.uid
    NavigationView {
        ZStack{
            HStack{
                switch bottomBarKubun{
                case 0:
                    Text(userID).foregroundColor(Color.red)
                case 1:
                    List{
                        ForEach(postLists, id: \.self){item in
                            HStack{
                                Text(item)
                                Text(documentId)
                                    .frame(maxWidth: .infinity, alignment: .trailing)
                            }
                            .swipeActions(edge: .trailing){
                                Button {
                                    db.collection("posts").document(documentId).delete()
                                    postLists = []
                                    gepo()
                                }label: {
                                    Image(systemName: "trash.fill")
                                }.tint(.red)
                            }
                        }
                    }
                case 2:
                    Text("tomo")
                case 3:
                    Text("sette")
                default:
                    Text("")
                }
            }
            .navigationTitle(topName)
            .navigationBarTitleDisplayMode(.inline)
            if(bottomBarKubun == 1){
                floatingButton
            }
        }
        .toolbar{
            ToolbarItemGroup(placement: .bottomBar){
                Button("行きたい店") {
                    self.topName = "行きたい店"
                    bottomBarKubun = 1
                    postLists = []
                    gepo()
                }
                Spacer()
                Button("友達") {
                    topName = "友達"
                    bottomBarKubun = 2
                }
                Spacer()
                Button("設定") {
                    topName = "設定"
                    bottomBarKubun = 3
                }
            }
        }
    }
}
var floatingButton: some View {
    
        VStack {
            // 2. ButtonをViewの下方に配置
            Spacer()
            HStack {
                TextField("行きたいところを入力してください\n", text: $new)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                // 3. Viewの右方に配置
                Spacer()
                Button(action: {
                    // ボタンを押した時のアクションを記載
                    if(new == ""){
                        errorMessage = "入力は必須です"
                    }else{
                        self.postText = self.new
                        db.collection("posts").document().setData([
                            "post" : postText,
                            "date" : Date(),
                            "user" : Auth.auth().currentUser!.uid,
                            "id": db.collection("posts").document().documentID
                        ])
                        postLists = []
                        self.new = ""
                        gepo()
                    }
                }) {
                    // 4. Buttonのデザインを作成。記号の"+"を配置。
                    Image(systemName: "plus.circle.fill")
                        // Fontサイズ
                        .font(.system(size: 24))
                        // Buttonの文字色
                        .foregroundColor(.white)
                        // Buttonのサイズ
                        .frame(width: 60, height: 60)
                        // Buttonの色
                        .background(Color.blue)
                        // Buttonの角の丸み
                        .cornerRadius(30.0)
                        // Buttonの陰
                        .shadow(color: .gray, radius: 3, x: 3, y: 3)
                        // Buttonの端からの距離
                        .padding(EdgeInsets(top: 0, leading: 0, bottom: 16.0, trailing: 20))
                }
            }
            Text(errorMessage)
        }
    }
}

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。