0

I am using egui to make GUI for my rust application using mongodb atlas . The problem is that the app connects to mongodb for signup and login page but for the home page it gives the following error.

Failed to get user: Kind: Server selection timeout: None of the available servers suitable for criteria ReadPreference(Primary).

Here is the code for homepage:

fn home_page(&mut self, ctx: &egui::CtxRef, ui: &mut egui::Ui) {
        ui.heading("Home Page");
        ui.add_space(10.0);
        ui.label(format!("Welcome, {}!", self.username)); // Display welcome message with user's name

        let rt= Runtime::new().unwrap();
        let userid = self.userid.clone();
        let another_uc = self.user_collection.clone().unwrap();
        let vc = self.voice_note_collection.clone().unwrap();
        let username= self.username.clone();
        let pass= self.password.clone();
       
        let response = rt.block_on( async move
            {
                let response = tokio::spawn
                ( async move
                    {
                        let response = db_config::get_user_by_username(another_uc ,username , pass ).await;

                        response
                    }
                ).await.unwrap();
                response
            });
            println!("{:? }" , response);
        ui.horizontal(|ui| {
            if ui.button("My Files").clicked() {
                // Redirect to My Files page
                //self.current_page = Page::MyFiles;
            }
            if ui.button("Shared Files").clicked() {
                // Redirect to Shared Files page
                //self.current_page = Page::SharedFiles;
            }
            if ui.button("theme").clicked() {
                //change mode to light mode
                self.toggle_theme(ctx);
            }
            if ui.button("Logout").clicked() {
                // Redirect to Login page and clear user data
                self.username.clear();
                self.password.clear();
                self.confirm_password.clear();
                self.error_message = None;
                self.current_page = Page::Login;
            }
        });
    }

I am sure the problem is with connection and not with db_config functions as I have tried for multiple functions.

Here is the full error:

Failed to get user: Kind: Server selection timeout: None of the available servers suitable for criteria ReadPreference(Primary). Topology: { Type: ReplicaSetWithPrimary, Set Name: atlas-13wx29-shard-0, Max Set Version: 8, Max Election ID: 7fffffff000000000000014d, Servers: [ { Address: ac-7hdslbx-shard-00-00.btmwmdh.mongodb.net:27017, Type: RsSecondary, Average RTT: 856.98116ms, Last Update Time: 2023-05-24 12:04:54.102 +00:00:00, Max Wire Version: 17, Min Wire Version: 0, Replica Set Name: atlas-13wx29-shard-0, Replica Set Version: 8, Tags: {"provider": "AWS", "nodeType": "ELECTABLE", "region": "US_EAST_1", "workloadType": "OPERATIONAL"} }, { Address: ac-7hdslbx-shard-00-02.btmwmdh.mongodb.net:27017, Type: RsSecondary, Average RTT: 875.57704ms, Last Update Time: 2023-05-24 12:04:54.111 +00:00:00, Max Wire Version: 17, Min Wire Version: 0, Replica Set Name: atlas-13wx29-shard-0, Replica Set Version: 8, Tags: {"nodeType": "ELECTABLE", "provider": "AWS", "workloadType": "OPERATIONAL", "region": "US_EAST_1"} }, { Address: ac-7hdslbx-shard-00-01.btmwmdh.mongodb.net:27017, Type: RsPrimary, Average RTT: 866.34186ms, Last Update Time: 2023-05-24 12:04:54.138

+00:00:00, Max Wire Version: 17, Min Wire Version: 0, Replica Set Name: atlas-13wx29-shard-0, Replica Set Version: 8, Tags: {"provider": "AWS", "region": "US_EAST_1", "workloadType": "OPERATIONAL", "nodeType": "ELECTABLE"} } ] }, labels: {}

I have tried multiple such as connecting to mongodb again it works but due to egui rendering page again and again it also connects to mongo db again and again which is not efficient.

7stud
  • 46,922
  • 14
  • 101
  • 127
glitch_123
  • 139
  • 12
  • Don't connect to the database in the rendering function. Interact with the database another way, such as on another thread or on a spawned async task. – cdhowie May 24 '23 at 17:23
  • I have function that connects to mongodb and returns me the collections that I would like to use later on. This function is called in login and signup, and I store the collections in the self object of Gui. – glitch_123 May 25 '23 at 08:57
  • Now on home page, I need to use those collections, but it gives error?? When I try and print the collection, once after saving it in login/sign up, and the in home page, they are printed differently. The second print has some random raw values. Why is that? Previously for testing, I would use one collection object and pass it to many functions in the main file(there was no self objects, I used the variable directly), and it worked just fine. – glitch_123 May 25 '23 at 08:57

0 Answers0