我正在学习 Rust 和 SurrealDB,我正在尝试查询用户,但遇到了一个奇怪的错误。
这是代码:
#[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,它给了我同样的例子。
您需要指定您期望从响应中获得的类型。你的实际选择是
Option<T>
,,,Vec<T>
还是动态的Value
。我假设您期待的东西看起来像您现有的User
结构。从文档中的示例来看,通常如下所示:
或者,如果您确实想内嵌注释类型,请像这样指定它: