Taki
Taki

Reputation: 3730

Exception Opening Socket kmongo and ktor

I recently started to learn ktor so that i can make my own api , i set up my server and everything is okey but now i want to apply this logic of saving data into a db and then simply load data from db and send it over to my server

private fun insertDetails(user : ArrayList<userCredentials>){
     val client = KMongo.createClient()
     var database = client.getDatabase("test")
     var collection = database.getCollection<ArrayList<userCredentials>>()
     CoroutineScope(Dispatchers.IO).launch {
         collection.insertOne(user)
     }
 }

*This is how i'm getting data from kmongo db

private suspend fun getData() : List<ArrayList<userCredentials>> {
    return KMongo.createClient().getDatabase("test")
        .getCollection<ArrayList<userCredentials>>()
        .find().toList()
}
com.mongodb.MongoSocketOpenException: Exception opening socket

i would like to ask if i'm doing it the right way

This is my scheme for that : save data in db -> get data from db -> send it over to my server m thank you

Upvotes: 1

Views: 257

Answers (2)

Ben-J
Ben-J

Reputation: 1125

Indeed you need to have mongo daemon running: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/#using-mongodb-from-10gen-builds

What I had to do:

brew tap mongodb/brew
brew install [email protected]
brew services start [email protected]

And then it worked.

Upvotes: 0

Hamza Maqsood
Hamza Maqsood

Reputation: 415

Make sure that the mongo daemon is running and the target port is opened in firewall.

Upvotes: 2

Related Questions