Replies: 9 comments
-
root@T14P:~/fastapi-project/fastapi-learn/loco-learn/myapp# cargo loco doctor For more information about this error, try |
Beta Was this translation helpful? Give feedback.
-
correct pub async fn create_recipe(State(ctx): State,Json(params): Json) ->Result |
Beta Was this translation helpful? Give feedback.
-
I had the same error and it caused me a lot of confusion I made this pull request to solve it and make the errors more readable. import this: use axum::debug_handler; and add this macro to your handler that is causing the issue like so. #[debug_handler] |
Beta Was this translation helpful? Give feedback.
-
documents : Building a CRUD API pub async fn update( Path(id): Path,State(ctx): State,Json(params): Json) -> Result { An error is reported when changing the order of function parameters. pub async fn update( Path(id): Path,Json(params): Json,State(ctx): State) -> Result { error[E0277]: the trait bound is not satisfied |
Beta Was this translation helpful? Give feedback.
-
Yes, the order matters for the parameters. If you add this macro #[debug_handler] to your function you will get better error messages. #[debug_handler] Like this but do not forget to import the following. use axum::debug_handler; |
Beta Was this translation helpful? Give feedback.
-
This design is very unreasonable.Is it possible to make the order of function parameters not affected? |
Beta Was this translation helpful? Give feedback.
-
For what's worth, I don't think the design is "very unreasonable" - like everything, it has tradeoffs. The loco server handlers are actually from a library called I'm not sure what you mean by "the misinformation is outrageous". Back to talking about tradeoffs, the I don't speak for loco, but it would be quite the undertaking to document all the libraries it uses on top of the loco specific behaviours. I WOULD agree that having a blurb in the documentation about order of extractors mattering, and a link to the respective documentation would be a net benefit. |
Beta Was this translation helpful? Give feedback.
-
pub async fn fetch_receipe(Path(id): Path, State(ctx): State) correct but async fn create_recipe(State(ctx): State,Json(params): Json) correct That's why it's puzzling. |
Beta Was this translation helpful? Give feedback.
-
Something subtle that is mentioned in the docs I linked above is the disclaimer that extractors who implement the In your first example, both |
Beta Was this translation helpful? Give feedback.
-
#![allow(clippy::unused_async)]
use loco_rs::prelude::*;
use sea_orm::DeleteResult;
use crate::models::_entities::recipes;
use crate::models::_entities::recipes::{ActiveModel};
use serde::Deserialize;
use axum::response::Json;
use axum::extract::Query;
use axum::extract::Path;
use serde::Serialize;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RecipeSearchParams {
pub limit: i32,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RecipeCreate {
pub label: Option,
pub source:Option,
pub url: Option,
}
impl RecipeCreate {
fn update(&self, item: &mut ActiveModel) {
item.label = Set(self.label.clone());
item.source = Set(self.source.clone());
item.url = Set(self.url.clone());
}
}
pub async fn fetch_receipe(Path(id): Path, State(ctx): State) -> Result{
let result = recipes::Entity::find_by_id(id).one(&ctx.db).await?;
}
pub async fn remove_recipe(Path(id): Path, State(ctx): State) -> Result {
// Delete the recipe from the database
let res: DeleteResult = recipes::Entity::delete_by_id(id).exec(&ctx.db).await?;
}
pub async fn search_recipes(Query(params): Query, State(ctx): State) -> Result {
let all_results = recipes::Entity::find().all(&ctx.db).await?;
let result :Vecrecipes::Model= all_results.into_iter().take(params.limit as usize).collect();
Ok(format::json(result)?)
}
pub async fn create_recipe(Json(params): Json, State(ctx): State) ->Result{
}
pub fn routes() -> Routes {
Routes::new()
.prefix("recipes")
.add("/:id", get(fetch_receipe))
.add("/remove_recipe/:id", get(remove_recipe))
.add("/search_recipes", get(search_recipes))
.add("/create_recipe", post(create_recipe))
}
---------------------------------------------------------error
root@T14P:~/fastapi-project/fastapi-learn/loco-learn/myapp# cargo loco doctor
Compiling myapp v0.1.0 (/root/fastapi-project/fastapi-learn/loco-learn/myapp)
error[E0277]: the trait bound
fn(axum::Json<RecipeCreate>, loco_rs::prelude::State<loco_rs::prelude::AppContext>) -> impl std::future::Future<Output = std::result::Result<axum::http::Response<Body>, loco_rs::Error>> {create_recipe}: Handler<_, _>
is not satisfied--> src/controllers/recipes.rs:82:37
|
82 | .add("/create_recipe", post(create_recipe))
| ---- ^^^^^^^^^^^^^ the trait
Handler<_, _>
is not implemented for fn itemfn(Json<RecipeCreate>, State<AppContext>) -> impl Future<Output = Result<Response<Body>, Error>> {create_recipe}
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait
Handler<T, S>
:<axum_extra::handler::or::Or<L, R, Lt, Rt, S> as Handler<(M, Lt, Rt), S>>
<axum_extra::handler::IntoHandler<H, T, S> as Handler<T, S>>
<Layered<L, H, T, S> as Handler<T, S>>
<MethodRouter
as Handler<(), S>>note: required by a bound in
loco_rs::prelude::post
--> /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.7.5/src/routing/method_routing.rs:389:1
|
389 | top_level_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^
| | |
| | required by a bound in this function
| 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)For more information about this error, try
rustc --explain E0277
.error: could not compile
myapp
(lib) due to 1 previous errorroot@T14P:~/fastapi-project/fastapi-learn/loco-learn/myapp#
Beta Was this translation helpful? Give feedback.
All reactions