I've been writing a simple crud app in Axum and Sea-orm, but I keep getting when trying to handle a Database Error.
Here's the create user function:
async fn create_user(Json(user_input): Json<User>) -> Result<Json<User>, Json<DbErr>> {
let mcrypt = new_magic_crypt!(env::var("SECRET_KEY").unwrap(), 256);
let db = db::DB::new().await;
let username = user_input.username;
let encrypted_password = mcrypt.encrypt_to_base64(&user_input.password);
let user_db = user::ActiveModel {
username: Set(username.clone().into()),
password: Set(encrypted_password.clone().into()),
..Default::default()
};
match user_db.insert(&db.db).await {
Ok(_) => Ok(Json(User {
username,
password: encrypted_password
})),
Err(e) => Err(Json(e))
}
}
And here's the the route:
.route("/api/create_user", post(create_user));
This is the error I get:
error[E0277]: the trait bound `fn(axum::Json<User>) -> impl Future<Output = Result<axum::Json<User>, axum::Json<sea_orm::DbErr>>> {create_user}: Handler<_, _, _>` is not satisfied
--> src\main.rs:35:41
|
35 | .route("/api/create_user", post(create_user));
| ---- ^^^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(axum::Json<User>) -> impl Future<Output = Result<axum::Json<User>, axum::Json<sea_orm::DbErr>>> {create_user}`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `Handler<T, S, B>`:
<Layered<L, H, T, S, B, B2> as Handler<T, S, B2>>
<MethodRouter<S, B> as Handler<(), S, B>>
note: required by a bound in `post`
--> C:\Users\Lobby\.cargo\registry\src\github.com-1ecc6299db9ec823\axum-0.6.18\src\routing\method_routing.rs:407:1
|
407 | top_level_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `post`
= note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)