【发布时间】:2021-08-20 23:44:28
【问题描述】:
我已从使用 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}`
经过一番谷歌搜索后,there is indeed a Handler trait 似乎在 actix 生态系统中(但是,我认为不是 actix-web)。
我不知道我需要在哪里实现这个特征。错误消息似乎表明函数本身缺少它,但我的理解是您只能在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上here。
【问题讨论】:
标签: rust actix-web rust-actix