I am trying to fetch my user post from firebase firestore but I am receiving an error
Cannot assign value of type ‘QuerySnapshot’ to type ‘[Post]’
func fetchPosts() async throws -> [Post] {
self.posts = try await FirestoreConstants
Argument passed to call that takes no arguments
.getDocuments(as: Post.self)
class FeedService {
private var posts = [Post]()
private let userService = ThirdUserService()
func fetchPosts() async throws -> [Post] {
self.posts = try await FirestoreConstants
.PostsCollection
.order(by: "timestamp", descending: true)
.getDocuments(as: Post.self)
await withThrowingTaskGroup(of: Void.self) { group in
for post in posts {
group.addTask { try await self.fetchPostUserData(post) }
}
}
return posts
}
PostData
import Firebase
import FirebaseFirestoreSwift
import SwiftUI
import AVKit
struct NewPost: Identifiable, Codable {
let id: String
let videoUrl: String
let ownerUid: String
let caption: String
var likes: Int
var commentCount: Int
var saveCount: Int
var shareCount: Int
var views: Int
var thumbnailUrl: String
var timestamp: Timestamp
var user: Userss?
var didLike = false
var didSave = false
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.videoUrl = try container.decode(String.self, forKey: .videoUrl)
self.ownerUid = try container.decode(String.self, forKey: .ownerUid)
self.caption = try container.decode(String.self, forKey: .caption)
self.likes = try container.decode(Int.self, forKey: .likes)
self.commentCount = try container.decode(Int.self, forKey: .commentCount)
self.saveCount = try container.decode(Int.self, forKey: .saveCount)
self.shareCount = try container.decode(Int.self, forKey: .shareCount)
self.views = try container.decode(Int.self, forKey: .views)
self.thumbnailUrl = try container.decode(String.self, forKey: .thumbnailUrl)
self.timestamp = try container.decode(Timestamp.self, forKey: .timestamp)
self.user = try container.decodeIfPresent(Userss.self, forKey: .user)
self.didLike = try container.decodeIfPresent(Bool.self, forKey: .didLike) ?? false
self.didSave = try container.decodeIfPresent(Bool.self, forKey: .didSave) ?? false
}
init(
id: String,
videoUrl: String,
ownerUid: String,
caption: String,
likes: Int,
commentCount: Int,
saveCount: Int,
shareCount: Int,
views: Int,
thumbnailUrl: String,
timestamp: Timestamp,
user: Userss? = nil,
didLike: Bool = false,
didSave: Bool = false
) {
self.id = id
self.videoUrl = videoUrl
self.ownerUid = ownerUid
self.caption = caption
self.likes = likes
self.commentCount = commentCount
self.saveCount = saveCount
self.shareCount = shareCount
self.views = views
self.thumbnailUrl = thumbnailUrl
self.timestamp = timestamp
self.user = user
self.didLike = didLike
self.didSave = didSave
}
}
extension NewPost: Hashable { }
extension NewPost: Equatable {
static func == (lhs: NewPost, rhs: NewPost) -> Bool {
return lhs.id == rhs.id
}
}




