Firestore打印多个单个文档的实例

发布于 2025-02-10 07:01:35 字数 2165 浏览 0 评论 0原文

尝试在子汇编(USERRECIPES)中打印所有文档

”“在此处输入图像说明”

我只有两个文档中存在两个文档 - 问题是当我打印到控制台时,它多次多次打印两个文档。下面的印刷品示例:

[“ flounder”:{ createAt =“< firtimestamp:seconsts = 1656102838 nanseconds = 66710000>“;”; 方向=(( 例子 ); ingreDientItem = { 示例=示例; }; 配方=“”; copeepreptime =“”; 配方=比目鱼; }]] [[“鸡蛋”:{ createAt =“< firtimestamp:seconsts = 1656102790 nanseconds = 482897000>“; 方向=(( ); ingreDientItem = { “ 1杯” =面粉; }; 配方=“”; copepreptime =“ 20分钟”; 食谱=鸡蛋; }]] [[“鸡蛋”:{ createAt =“< firtimestamp:seconsts = 1656102790 nanseconds = 482897000>“; 方向=(( ); ingreDientItem = { “ 1杯” =面粉; }; 配方=“”; copepreptime =“ 20分钟”; 食谱=鸡蛋; }], [“比目鱼”: { createAt =“< firtimestamp:seconsts = 1656102838 nanseconds = 66710000>“;”; 方向=(( 例子 ); ingreDientItem = { 示例=示例; }; 配方=“”; copeepreptime =“”; 配方=比目鱼; ]]]

下面是我的代码:

class RecipeLogic: ObservableObject {
    @Published var recipes = [RecipeItems]()
    @Published var recipeList = []
    init(){
     grabRecipes()
    }
    
    private func grabRecipes(){
        
        
        guard let uid = FirebaseManager.shared.auth.currentUser?.uid else {
            return
        }
        
        FirebaseManager.shared.firestore
            .collection("users")
            .document(uid)
            .collection("userRecipes")
            .getDocuments() { (snapshot, err) in
                if let err = err {
                    print("error")
                }
                else{
                    for recipe in snapshot!.documents {
                        self.recipeList.append(recipe.data())
                        print(self.recipeList)
                    }
                }
            }
        }
    }

struct RecipeItems: Codable, Hashable{
    var recipeTitle, recipePrepTime, recipeImage, createdAt: String
    var directions, ingredientItem: [String]
}

struct RecipeModel:Codable, Identifiable {
   var id: String = UUID().uuidString
    var recipes: [RecipeItems]
}

Trying to print all documents in a subcollection (userRecipes)

enter image description here

I only have two documents present in the subcollection - Issue is when I print to console, It's printing the two documents multiple times in excess. Example of the prints below:

["Flounder": {
createdAt = "<FIRTimestamp: seconds=1656102838 nanoseconds=66710000>";
directions = (
Example
);
ingredientItem = {
Example = Example;
};
recipeImage = "";
recipePrepTime = "";
recipeTitle = Flounder; }]] [["eggs": {
createdAt = "<FIRTimestamp: seconds=1656102790 nanoseconds=482897000>";
directions = (
);
ingredientItem = {
"1 cup" = Flour;
};
recipeImage = "";
recipePrepTime = "20 Mins";
recipeTitle = eggs; }]] [["eggs": {
createdAt = "<FIRTimestamp: seconds=1656102790 nanoseconds=482897000>";
directions = (
);
ingredientItem = {
"1 cup" = Flour;
};
recipeImage = "";
recipePrepTime = "20 Mins";
recipeTitle = eggs; }], ["Flounder": {
createdAt = "<FIRTimestamp: seconds=1656102838 nanoseconds=66710000>";
directions = (
Example
);
ingredientItem = {
Example = Example;
};
recipeImage = "";
recipePrepTime = "";
recipeTitle = Flounder; }]]

Below is my code:

class RecipeLogic: ObservableObject {
    @Published var recipes = [RecipeItems]()
    @Published var recipeList = []
    init(){
     grabRecipes()
    }
    
    private func grabRecipes(){
        
        
        guard let uid = FirebaseManager.shared.auth.currentUser?.uid else {
            return
        }
        
        FirebaseManager.shared.firestore
            .collection("users")
            .document(uid)
            .collection("userRecipes")
            .getDocuments() { (snapshot, err) in
                if let err = err {
                    print("error")
                }
                else{
                    for recipe in snapshot!.documents {
                        self.recipeList.append(recipe.data())
                        print(self.recipeList)
                    }
                }
            }
        }
    }

struct RecipeItems: Codable, Hashable{
    var recipeTitle, recipePrepTime, recipeImage, createdAt: String
    var directions, ingredientItem: [String]
}

struct RecipeModel:Codable, Identifiable {
   var id: String = UUID().uuidString
    var recipes: [RecipeItems]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2025-02-17 07:01:35

您您正在打印referalist在每次循环的每次迭代中,每次获得更新版本时,都只能打印一次firebase文档,您可以通过:

for recipe in snapshot!.documents {
      self.recipeList.append(recipe.data())
}
if let documents = snapshot.documents {
  // Your documents
  print(documents)
}

You're printing recipeList on every iteration of your for loop, each time getting an updated version, to print the documents of Firebase just once, you do so by:

for recipe in snapshot!.documents {
      self.recipeList.append(recipe.data())
}
if let documents = snapshot.documents {
  // Your documents
  print(documents)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文