首页 > 解决方案 > Swift - Firestore 自动创建集合/文档

问题描述

有没有办法自动创建集合或文档?

就我而言,我已经有一个collection(“用户”),其中包含所有用户。我想要实现的是,如果用户点击了几个按钮,并且还应该创建subcollections一个空按钮。document

我试过这个但失败了:

func insertWish(){
    let db = Firestore.firestore()
    let userID = Auth.auth().currentUser!.uid
    db.collection("users").document(userID).collection("wishlists").document("Main Wishlist").collection("Wünsche").document("Laptop").setData(["": ""]) { (error) in
        if error != nil {
            print("error")
        }
    }
}

这些集合/文档在我的数据库中不存在,只有在用户单击按钮时才应创建:

collection("wishlists").document("Main Wishlist").collection("Wünsche").document("Laptop")

标签: iosswiftfirebasegoogle-cloud-firestore

解决方案


错误消息“The property.name is the empty string”告诉您您正在尝试添加无效文档:

setData(["": ""])

空字符串的字段名称无效。给它一个符合文档要求的名称。


推荐阅读