Reputation: 1
I am trying to create a Function to fetch conversations for a user in SwiftUIachat app frontend to a Supabase db instance. Unfortunately, Xcode produces the following error message: "Extra trailing closure passed in call" when I declare the func fetchConversations that you will find below.
I would be grateful for any help.
import Foundation
import Supabase
class SupabaseManager {
static let shared = SupabaseManager()
let client: SupabaseClient
private init() {
let supabaseUrl = URL(string: "https://mysite.supabase.co")! // Replace with your Supabase URL
let supabaseKey = "supabaseKey" // Replace with your Supabase public API key
self.client = SupabaseClient(supabaseURL: supabaseUrl, supabaseKey: supabaseKey)
}
func registerWithPhone(email: String, password: String, completion: @escaping (Result<User, Error>) -> Void) {
Task {
do {
let authResponse = try await client.auth.signUp(email: email, password: password)
let user = authResponse.user
completion(.success(user))
} catch {
completion(.failure(error))
}
}
}
func loginWithPhone(email: String, password: String, completion: @escaping (Result<User, Error>) -> Void) {
Task {
do {
let authResponse = try await client.auth.signIn(email: email, password: password)
let user = authResponse.user
completion(.success(user))
} catch {
completion(.failure(error))
}
}
}
func logout(completion: @escaping (Result<Void, Error>) -> Void) {
Task {
do {
try await client.auth.signOut()
completion(.success(()))
} catch {
completion(.failure(error))
}
}
}
// Function to fetch conversations for a user
func fetchConversations(for userId: UUID, completion: @escaping (Result<[Conversation], Error>) -> Void) {
client.database
.from("Chats")
.select("chat_id, created_by, created_at")
.eq("created_by", value: userId.uuidString)
.execute { result in
switch result {
case .success(let response):
do {
let conversations = try JSONDecoder().decode([Conversation].self, from: response.data)
completion(.success(conversations))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
I have tried re-formulate the func declaration without any luck.
Any help would be great.
Many thanks
Upvotes: 0
Views: 64