我已经从使用actix-web 3.x.x转到了4.x.x。以前运行得非常好的代码现在抛出了这个错误:
the trait bound `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}: Handler<_, _>` is not satisfied
--> src/routes/all_routes.rs:74:14
|
74 | pub async fn tweets4(
| ^^^^^^^ the trait `Handler<_, _>` is not implemented for `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}`在谷歌上搜索了一下后,actix生态系统中似乎出现了actix(不过,我认为并不是actix)。
我不知道我需要在哪里实现这一特性。错误消息似乎表明函数本身缺少它,但我的理解是,您只能在structs和enums上实现特性,而不是函数?
下面是处理程序代码:
#[get("/tweets4")]
pub async fn tweets4(
form: web::Query<TweetParams>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, HttpResponse> {
let fake_json_data = r#"
{ "name": "hi" }
"#;
let v: Value = serde_json::from_str(fake_json_data)
.map_err(|_| HttpResponse::InternalServerError().finish())?;
sqlx::query!(
r#"
INSERT INTO users
(id, created_at, twitter_user_id, twitter_name, twitter_handle, profile_image, profile_url, entire_user)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
"#,
Uuid::new_v4(),
Utc::now(),
"3",
"4",
"5",
"6",
"7",
v,
)
.execute(pool.as_ref())
.await
.map_err(|e| {
println!("error is {}", e);
HttpResponse::InternalServerError().finish()
})?;
Ok(HttpResponse::Ok().finish())
}我做错什么了?
如果有帮助,整个项目都在github 这里上。
发布于 2021-06-03 05:33:04
经过充分的试验和错误,我发现:
HttpResponse错误类型,我不得不用自己的替换:#[derive(Debug)]
pub struct MyError(String); // <-- needs debug and display
impl std::fmt::Display for MyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "A validation error occured on the input.")
}
}
impl ResponseError for MyError {} // <-- key
#[get("/tweets4")]
pub async fn tweets4(
form: web::Query<TweetParams>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, MyError> {
let fake_json_data = r#"
{ "name": "hi" }
"#;
let v: Value = serde_json::from_str(fake_json_data).map_err(|e| {
println!("error is {}", e);
MyError(String::from("oh no")) // <-- here
})?;
sqlx::query!(
//query
)
.execute(pool.as_ref())
.await
.map_err(|e| {
println!("error is {}", e);
MyError(String::from("oh no")) // <-- and here
})?;
Ok(HttpResponse::Ok().finish())
}希望将来能帮助别人!
发布于 2022-05-21 17:08:04
如果您仍然面临相同的错误,另一个解决方案就是切换到1.0.8
https://stackoverflow.com/questions/67811502
复制相似问题