Trait bound in Actix routes

huangapple go评论95阅读模式
英文:

Trait bound in Actix routes

问题

以下是您提供的文本的翻译部分:

I'm making an API that already works with a few entities. While that part compiles, runs and works okay, I'm having serious problems when adding users: Actix doesn't seem to like the two routes related to them (create_user and user_login). This is my route declaration:

我正在创建一个API,它已经可以与一些实体一起正常工作。尽管这部分可以编译、运行并正常工作,但当添加用户时,我遇到了严重的问题:Actix似乎不喜欢与它们相关的两个路由(create_user和user_login)。这是我的路由声明:

And the error I have is the following:

我遇到的错误如下:

the trait bound fn(actix_web::web::Json<LoginData>, Data<Pool<ConnectionManager<diesel::SqliteConnection>>>) -> HttpResponse {user_login_handler}: Handler<_> is not satisfied
the trait Handler<_> is not implemented for fn(actix_web::web::Json<LoginData>, Data<Pool<ConnectionManager<diesel::SqliteConnection>>>) -> HttpResponse {user_login_handler}

错误是:

特质约束 fn(actix_web::web::Json<LoginData>, Data<Pool<ConnectionManager<diesel::SqliteConnection>>>) -> HttpResponse {user_login_handler}: Handler<_> 未满足
特质 Handler<_> 未实现于 fn(actix_web::web::Json<LoginData>, Data<Pool<ConnectionManager<diesel::SqliteConnection>>>) -> HttpResponse {user_login_handler}

This is the full code of usres_handlers.rs, where you can see the create_user and login_user functions:

这是 usres_handlers.rs 的完整代码,您可以在其中看到 create_userlogin_user 函数:

What am I doing wrong here?

我在这里做错了什么?

英文:

I'm making an API that already works with a few entities. While that part compiles, runs and works okay, I'm having serious problems when adding users: Actix doesn't seem to like the two routes related to them (create_user and user_login). This is my route delcaration:

  1. HttpServer::new(move || {
  2. App::new()
  3. .app_data(actix_web::web::Data::new(pool.clone()))
  4. // User class
  5. .route(
  6. "/create_user",
  7. web::post().to(users_handlers::create_user_handler),
  8. )
  9. .route(
  10. "/login",
  11. web::post().to(users_handlers::user_login_handler),
  12. )

And the error I have, is the following:

  1. the trait bound `fn(actix_web::web::Json<LoginData>, Data<Pool<ConnectionManager<diesel::SqliteConnection>>>) -> HttpResponse {user_login_handler}: Handler<_>` is not satisfied
  2. the trait `Handler<_>` is not implemented for `fn(actix_web::web::Json<LoginData>, Data<Pool<ConnectionManager<diesel::SqliteConnection>>>) -> HttpResponse {user_login_handler}`rustcClick for full compiler diagnostic
  3. main.rs(48, 29): required by a bound introduced by this call
  4. route.rs(211, 12): required by a bound in `Route::to`

This is the full code of usres_handlers.rs, where you can see the create_user and login_user functions:

  1. use crate::{
  2. models::{User, LoginData},
  3. utils::{log, LogType},
  4. };
  5. use actix_web::{http::header::ContentType, web, HttpResponse};
  6. use diesel::{r2d2::ConnectionManager, SqliteConnection};
  7. mod users_dao;
  8. pub type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
  9. /**
  10. * HTTP Handlers for the users API
  11. * This layer is responsible for handling the HTTP requests and responses,
  12. * keep log of the errors, and call the DAO layer to interact with the database.
  13. *
  14. */
  15. pub fn create_user_handler(user_data: web::Json<User>, pool: web::Data<DbPool>) -> HttpResponse {
  16. match (validate_user(user_data)) {
  17. Ok(user) => {
  18. let result = users_dao::create_user(user_data, pool);
  19. match result {
  20. Ok(user) => HttpResponse::Ok()
  21. .content_type(ContentType::json())
  22. .json(&user),
  23. Err(err) => {
  24. println!("create_user_handler, ERROR: {:#?}", err);
  25. log(LogType::Error, err.to_string());
  26. HttpResponse::InternalServerError()
  27. .content_type(ContentType::json())
  28. .body("{err: 'Unable to insert user into database'")
  29. }
  30. }
  31. }
  32. Err(err) => {
  33. log(LogType::Error, err.to_string());
  34. HttpResponse::BadRequest()
  35. .content_type(ContentType::json())
  36. .body("{err: 'Unable to validate user data'")
  37. }
  38. }
  39. }
  40. pub fn user_login_handler(
  41. user_data: web::Json<LoginData>,
  42. pool: web::Data<DbPool>
  43. ) -> HttpResponse {
  44. let result = users_dao::user_login(user_data, pool);
  45. match (result) {
  46. Ok(user) => {
  47. log(LogType::Info, format!("User {} logged in", user.username));
  48. HttpResponse::Ok()
  49. .content_type(ContentType::json())
  50. .json(&user)
  51. },
  52. Err(err) => {
  53. log(LogType::Error, err.to_string());
  54. HttpResponse::BadRequest()
  55. .content_type(ContentType::json())
  56. .body("{err: 'Wrong username or password'")
  57. }
  58. }
  59. }
  60. // Private functions
  61. fn validate_user(user: web::Json<User>) -> Result<web::Json<User>, &'static str> {
  62. if user.into_inner().username.is_empty() {
  63. return Err("Username is required");
  64. }
  65. if user.into_inner().password.is_empty() {
  66. return Err("Password is required");
  67. }
  68. Ok(user)
  69. }

What am I doing wrong here?

答案1

得分: 2

尝试将两个函数都标记为asyncHandler要求函数的输出实现Future特性。

英文:

Maybe try and make both functions async? Handler requires the output of a function to implement the Future trait.

huangapple
  • 本文由 发表于 2023年2月23日 19:14:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定