簡體   English   中英

Firebase Swift:如何使用“用戶”集合下的 UID 創建新集合?

[英]Firebase Swift: How do I create a new collection using the UID under my “users” collection?

我正在使用 Firebase 身份驗證和雲數據庫創建一個新的 IOS 應用程序。 添加新用戶時,會創建一個“個人資料”集合,其中包含他們的初始注冊輸入。 我需要弄清楚如何在使用 UID 作為其文檔 ID 的“用戶”集合下創建一個新集合。

到目前為止,這是我為此位編寫的代碼,但我不斷收到此消息"Value of type 'CollectionReference' has no member 'child'" 我遵循了 Firebase 說明,但我一直弄錯。

我錯過了什么?

        // Create the user
        Auth.auth().createUser(withEmail: email, password: password) { (result, err) in

            // Check for errors
            if err != nil {

                // There was an error
                self.showError("Error creating the user")
            }
            else {
                // User was created successfully, now store the first name and last name
                let db = Firestore.firestore()

                let uid = result!.user.uid


                //creates profile doc under uid with all the info
                db.collection("users").child(uid).addDocument("profile").setData([ "firstname":firstName, "lastname":lastName, "uid":uid, "companyname":companyName, "companyalias":companyAlias, "trucknumber":truckNumber, "trucktype":truckType]) { (error) in

                //db.collection("users").addDocument(data: ["firstname":firstName, "lastname":lastName, "uid":result!.user.uid ]) { (error) in

                    if error != nil {
                        //Show error message
                        self.showError("Error saving user data")
                    }
                }

                //Transition to home
                self.transitionToHome()
            }
        }
    }
}

我擔心在創建新用戶時我可能應該在開始時創建其他 collections。 我需要每個用戶都有一個保存在 UID 下的“個人資料”、“朋友列表”和“我的工作”集合。

我是否應該在開始時擔心是否創建所有必需的 collections?

正如弗蘭克·范·普菲倫所說。 您可以簡單地將用戶數據用作您的配置文件數據,然后根據需要添加子集合。 創建用戶時無需創建子集合。 在我的示例中,僅當用戶使用應用程序的其他功能時才會創建子集合。 我會添加用戶個人資料所需的所有不同字段,然后讓用戶稍后修改它們,但這只是我的偏好。 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM