代码之家  ›  专栏  ›  技术社区  ›  Ayush Gupta

SurralDB响应采取方法不起作用

  •  0
  • Ayush Gupta  · 技术社区  · 2 年前

    我正在学习Rust和SurralDB,并试图查询用户,但我遇到了一个奇怪的错误。

    这是代码:

    #[post("/register")]
    async fn register_user_handler(
        body: web::Json<RegisterUserSchema>,
        data: web::Data<AppState>,
    ) -> impl Responder {
        let result = data
            .db
            .query("SELECT * from users where email = $email LIMIT 1")
            .bind(("email", &body.email))
            .await
            .unwrap();
    
        println!("From here: {:#?}", result.take(0));  // <------- This is the error line
    
        let uuid_id = Uuid::new_v4();
    
        let user = User {
            _id: uuid_id.to_string(),
            name: body.name.to_owned(),
            username: body.username.to_owned(),
            email: body.email.to_owned().to_lowercase(),
            password: "".to_string(),
            provider: "".to_string(),
            age: None,
            phone: "".to_string(),
            photo: "".to_string(),
            location: "".to_string(),
        };
    
        let new_user: Result<Vec<Option<User>>, Error> = data.db.create("users").content(user).await;
    
        if new_user.is_err() {
            println!("{:#?}", new_user);
        }
    
        println!("{:#?}", new_user);
    
        let json_response = serde_json::json! ({
            "status": "success".to_string(),
        });
    
        HttpResponse::Ok().json(json_response)
    }
    

    我得到了这个错误,这根本没有任何意义,因为它在文档和我看到的所有示例中都很好。我在谷歌上搜索了这个错误,但一无所获。我尝试了ChatGPT,但还是出现了同样的错误。

    error[E0277]: the trait bound `i32: QueryResult<_>` is not satisfied
       --> src\app\users\views.rs:37:46
        |
    37  |     println!("From here: {:#?}", result.take(0));
        |                                         ---- ^ the trait `QueryResult<_>` is not implemented for `i32`
        |                                         |
        |                                         required by a bound introduced by this call
        |
        = help: the following other types implement trait `QueryResult<Response>`:
                  <usize as QueryResult<Vec<T>>>
                  <usize as QueryResult<surrealdb::sql::Value>>
                  <usize as QueryResult<std::option::Option<T>>>
    note: required by a bound in `surrealdb::Response::take`
       --> C:\Users\Ayush\.cargo\registry\src\index.crates.io-6f17d22bba15001f\surrealdb-1.4.2\src\api\method\query.rs:344:40
        |
    344 |     pub fn take<R>(&mut self, index: impl opt::QueryResult<R>) -> Result<R>
        |                                           ^^^^^^^^^^^^^^^^^^^ required by this bound in `Response::take`
    

    我在谷歌上搜索了这个错误,查看了文档和一些示例,发现实现没有任何差异。我问了ChatGPT,它给了我同样的例子。

    1 回复  |  直到 2 年前
        1
  •  1
  •   kmdreko    2 年前

    您需要指定期望从响应中获得的类型。您的实际选择是 Option<T> , Vec<T> ,或动态 Value 。我想你期待的是一件看起来像你现有的东西 User 结构。

    来自 examples in the documentation 通常看起来是这样的:

    let user: Option<User> = result.take(0).unwrap();
    println!("From here: {:#?}", user);
    

    或者,如果您真的想对类型进行内嵌注释,可以这样指定:

    println!("From here: {:#?}", result.take::<Option<User>>(0));
    
    推荐文章