I'm building a minimal web server with tokio/axum and MongoDB.
I have a file src/config/database.rs
containing my MongoDB configuration
use mongodb::{options::ClientOptions, Client};
use std::env;
pub async fn mongodb() -> mongodb::Client {
let database_uri = env::var("DATABASE_URI").expect("error reading database URI");
let database_name = env::var("DATABASE_NAME").expect("error reading database name");
// Get a handle to the cluster and Ping the server to see if you can connect to the cluster
let database_client_options = ClientOptions::parse(&database_uri)
.await
.expect("err connecting to database");
let database_client =
Client::with_options(database_client_options).expect("unable to connect to db");
database_client.database(&database_name);
println!("Successfully Connected to Database.");
//return database client
database_client
}
I imported this configuration into my src/main.rs
an initialize the config thus
...
config::database::mongodb().await;
mod config;
I want to be able to perform CRUD operation in src/controllers/auth.rs
thus
use crate::{config::database::mongodb, shared::user_schema::User};
///create a new user
pub async fn sign_up(Json(payload): Json<User>) -> impl IntoResponse {
let database = mongodb().await;
let collection = database.collection::<User>("user").await;
...
}
But I'm getting the following error
Compiling nitrogen v0.1.0 (/home/drizzle/devs/nitrogen)
error[E0599]: no method named `collection` found for struct `Client` in the current scope
--> src/controllers/auth.rs:12:31
|
12 | let collection = database.collection::<User>("user").await;
| ^^^^^^^^^^ method not found in `Client`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `nitrogen` due to previous error
[Finished running. Exit status: 101]
I really don't know what I'm doing wrong please see the full project on GitHub