-1

I'm developing a mobile app using Swift and Realm database.

I configured Realm Device Sync and tried to add custom user data to a cluster I created.

Even though I watched dozens of tutorials about realm permissions I still can't figure out what's wrong with the in-app permissions

here is the authentication function I am using to add Custom User Data

func login() {
        isLoading = true
        errorMessage = nil
        
        
        let credentials = Credentials.emailPassword(email: username, password: password)
        
        DispatchQueue.main.async {
            app.login(credentials: credentials) { [weak self] result in
                switch (result) {
                case .failure(let error):
                    print(String(describing: error))
                    self?.errorMessage = error.localizedDescription
                    
                case .success(let user):
                    if user.customData.isEmpty {
                        let client = user.mongoClient("mongodb-atlas")
                        let database = client.database(named: "UserAPI")
                        let collection = database.collection(withName: "Users")
                        // Insert the custom user data object
                        let customUserData: Document = [
                            "_id": AnyBSON(user.id),
                            "email": .string(self!.email),
                            "province": .string(self!.province),
                            "_partition": .string(user.id)
                        ]
                        collection.insertOne(customUserData) {result  in
                            switch result {
                            case .failure(let error):
                                print("Failed to insert document: \(error.localizedDescription)")
                            case .success(let newObjectId):
                                print("Inserted custom user data document with object ID: \(newObjectId)")
                            }
                        }
                    }
                }
                self?.isLoading = false
            }
        }
    }

But when I try to create a new user, it successfully creates one. The problem is, when it comes things comes to adding the Custom User Data it returns an error like this:

Failed to insert document: no rule exists for namespace 'UserAPI.Users'

and when I check the MongoDB logs, I can see the error in more detail: and when I check the MongoDB logs, I can see the error in more detail:

my Custom User Data settings: enter image description here

and my app permissions: enter image description here

any help would be appriciated, I'm struggling with this error for 3 days, thanks in advance.

grandsirr
  • 584
  • 4
  • 19
  • 1
    For testing, set your Read and Write permissions to true and try it again. – Jay Jun 16 '22 at 17:54
  • it didn't work too, I don't think the problem is with permissions because I tried everything about permissions – grandsirr Jun 24 '22 at 11:06

2 Answers2

0

@GrandSirr - have you tried setting "users can read and write all data" permissions template (for development, at least)?

Also, what is your actual 'Users' collection? User custom data should be a separate collection in my opinion as size of user custom data is limited.

My flow - login users with email password - set database triggers to create a new user document with relevant fields that a user can later fill in eg 'profile settings' page of your app and another trigger to create a document in a separate user custom data collection to save some permission roles or subscription to notifications etc.

0

I just got the same problem as you. that is on monggo db logs get error enter image description here

but I use Next.js with code like this

    const app = new Realm.App({ id: '' });
    const credentials = Realm.Credentials.anonymous();

    try {
      const user = await app.logIn(credentials);

      const searchPrompts = await user.functions.searchPrompt("web")
      setPost(() => searchPrompts)
      console.log(searchPrompts)

    } catch (err) {
      console.error("Failed to log in", err);
    }

I fixed the error by changing the rules on the data like this enter image description here