#Upsert Announcement: Swift Not Working

19 messages · Page 1 of 1 (latest)

wind arrow
#

With the Upsert announcement, we are not having success with upsert in Swift. We’ve tried the following

(from the docs)

do {
    let doc = try await databases.upsertDocument(
        databaseId: databaseId,
        collectionId: collectionId,
        documentId: ID.unique(),
        data: ["title": "New Title", "author": "New Author"]
    )
} catch {
    print(error.localizedDescription)
}

and also

let bookToUpsert = Book(id: docId, title: "New Title", author: "New Author")

do {
    let doc = try await databases.upsertDocument(
        databaseId: databaseId,
        collectionId: collectionId,
        documentId: docId,
        data: bookToUpsert,
        nestedType: Book.self
    )
    print(doc)
} catch {
    print(error.localizedDescription)
}

We have verified read and write permissions are enabled (we can create, read, update and delete documents)

Swift, macOS 15.3, Apple SDK 10.1.1

the result is simply

Server Error

in the console, with no details

Perhaps we are doing something wrong in our code? Also, what's the situation with ID.unique() - if the id is always unique won't it always create a new document?

rare oak
rare oak
wind arrow
#

Sorry - forgot to add the backticks. Edited.

wind arrow
#

68177f310000459b2474

rare oak
wind arrow
#

I was using the above code snippits. The first from above uses ID.unique() and the second where docId var was equal to "1"

#

There are no documents in the collection so I would have expected one of those code snippets - or both - to add that documents

rare oak
#

can you try passing something for the permissions param?

wind arrow
#

Sure. GIve me a sec

#

I don't think the permissions param is documented for swift.

#

I am working on it still though. Just need to format it correctly.

#

So the permissions need a user id parameter, which I don't have a user id.

#

I tried this permissions: [Permission.update(Role.user("guests"))] but the error in the console is Permissions must be one of: (any, guests)

#

As a test, I created user account so I could try a user id permissions: [Permission.update(Role.user("686d81610029c2f4c455"))] but the same error appeared in console: Permissions must be one of: (any, guests)

rare oak
wind arrow
#

Yep. That did it