简体   繁体   English

如何通过查看 UID 在 Firestore 数据库中设置新字段

[英]How to set a new field in a firestore database by looking at the UID

在此处输入图像描述 I'm trying to add a profilePicturePath to a firebase database record, but I can't seem to add it to an existing record in the database.我正在尝试将 profilePicturePath 添加到 firebase 数据库记录中,但我似乎无法将其添加到数据库中的现有记录中。

my current code is我目前的代码是

let db = Firestore.firestore();
            
db.collection("users").document(user?.uid ?? "").setData(["profilePictureURL" : uploadRef.fullPath])

The user id stored locally is a field in the firebase record.本地存储的用户id是firebase记录中的一个字段。

the profilePicutreURL does not exist as a field in the firebase record at the point im trying to insert.在我尝试插入时,profilePicutreURL 不作为 firebase 记录中的字段存在。 (not even sure if that matters) (甚至不确定这是否重要)

in sudocode id imagine its something like在 sudocode id 中想象它类似于

database.getCollection("Users").getRecordWithFieldValue("uid" : "localUserObject.uid").setField("profilePicture" : uploadRef.fullPath"

Really sorry if this is a stupid question, very new to IOS and firebase.如果这是一个愚蠢的问题,真的很抱歉,对于 IOS 和 firebase 来说非常新。

any help or guidance on this problem, or any of my other shoddy code would be greatly appreciated任何关于这个问题的帮助或指导,或者我的任何其他伪劣代码将不胜感激

This is how I would do it我会这样做

guard let uid = Auth.auth().currentUser?.uid else { return }

let db = Firestore.firestore()
let userRef = db.collection("users").document(uid)

userRef.setData(["profilePictureURL" : uploadRef.fullPath])

Ok, so you need to use something like this.好的,所以你需要使用这样的东西。 First a query will return an optional QuerySnapshot which you need to get the docs from and iterate through them to get the ID.首先,查询将返回一个可选的 QuerySnapshot,您需要从中获取文档并遍历它们以获取 ID。 There should only be one UID but I don't know how you set this up.应该只有一个 UID,但我不知道你是如何设置的。 From there you have a docID which you can use within a DocumentReference using the updateData method not setData.从那里你有一个 docID,你可以使用 updateData 方法而不是 setData 在 DocumentReference 中使用它。

 func getUserDocIDWith(uid: String, completion: @escaping (String) -> ()) {
        DispatchQueue.global(qos: .userInitiated).async {
            let usersRef = db.collection("users").whereField("uid", isEqualTo: uid)
        
        usersRef.getDocuments { (snapshot, error) in
            if error != nil {
                print("Error: \(error!.localizedDescription)")
            } else {
                let docs = snapshot!.documents
                for each in docs {
                    completion(each.documentID)
                }
            }
        }
    }
}

func updateUserWith(path: String) {
    let db = Firestore.firestore()
    guard let uid = Auth.auth().currentUser?.uid else { return }
    
    getUserDocIDWith(uid: uid) { docID in
        let usersRef = db.collection("users").document(docID)
        usersRef.updateData(["profilePictureURL" : path])
    }
}

updateUserWith(path: uploadRef.fullPath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM