Firestore打印多个单个文档的实例
尝试在子汇编(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)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您您正在打印
referalist
在每次循环的每次迭代中,每次获得更新版本时,都只能打印一次firebase文档,您可以通过: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: