I am not sure if I am returning the correct type and I am not sure how to figure that out. I basically want to get a response with the selected data.
Compiler gives me error[E0277] and mentions something about the Handler trait not being satisfied, but I am not sure if that is the actual problem.
error[E0277]: the trait bound `fn(Extension<Pool<sqlx::Postgres>>) -> impl Future<Output = Result<Vec<String>, (StatusCode, String)>> {using_connection_pool_extractor}: Handler<_, _>` is not satisfied
--> src/main.rs:52:17
|
52 | get(using_connection_pool_extractor).post(using_connection_extractor),
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Handler<_, _>` is not implemented for `fn(Extension<Pool<sqlx::Postgres>>) -> impl Future<Output = Result<Vec<String>, (StatusCode, String)>> {using_connection_pool_extractor}`
| |
| required by a bound introduced by this call
|
= help: the trait `Handler<T, ReqBody>` is implemented for `axum::handler::Layered<S, T>`
note: required by a bound in `axum::routing::get`
The reason why is that there's no implementation of the IntoResponse trait for Vec<T>. Here's a longer answer by Axum's author: Axum error handling/trait question
let db_connection_str = std::env::var("DATABASE_URL")
.unwrap_or_else(|_| "postgres://postgres:postgres@localhost".to_string());
// setup connection pool
let pool = PgPoolOptions::new()
.max_connections(5)
.connect_timeout(Duration::from_secs(3))
.connect(&db_connection_str)
.await
.expect("can connect to database");
the trait bound `axum::Json<Vec<String>>: Type<sqlx::Postgres>` is not satisfied
--> src/main.rs:69:5
|
69 | sqlx::query_scalar("SELECT * FROM todos")
| ^^^^^^^^^^^^^^^^^^ the trait `Type<sqlx::Postgres>` is not implemented for `axum::Json<Vec<String>>`
Thanks. That got rid of the Handler error, but now I am getting the following compiler error.
the trait bound `axum::Json<Vec<String>>: sqlx::Decode<'_, sqlx::Postgres>` is not satisfied
the following other types implement trait `sqlx::Decode<'r, DB>`:
<&'r [u8] as sqlx::Decode<'r, sqlx::Any>>
<&'r [u8] as sqlx::Decode<'r, sqlx::Postgres>>
<&'r serde_json::raw::RawValue as sqlx::Decode<'r, DB>>
<&'r str as sqlx::Decode<'r, sqlx::Any>>
<&'r str as sqlx::Decode<'r, sqlx::Postgres>>
<() as sqlx::Decode<'r, sqlx::Postgres>>
<(T1, T2) as sqlx::Decode<'r, sqlx::Postgres>>
<(T1, T2, T3) as sqlx::Decode<'r, sqlx::Postgres>>
and 34 others
required because of the requirements on the impl of `for<'r> FromRow<'r, PgRow>` for `(axum::Json<Vec<String>>,)`rustcE0277
query_scalar.rs(82, 26): required by a bound in `QueryScalar::<'q, DB, O, A>::fetch_one`
Not sure what it is asking me to do or if it is giving the right error.
Basically, the handler function only accepts things that implement the IntoResponse trait. So either I need to create my own type that implements IntoResponse and pass that in as the return type for the Result or use a type that already implements IntoResponse like axum::Json<Vec<String>> suggested by moy2010.
So if I try to use built in axum types that already implement IntoResponse, the problem will be converting the Result of sqlx::query into that type.