I am learning Rust and SurrealDB and I am trying to query an user but I am getting a bizzare error.
Here’s the code:
#[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
// if result() {
// return HttpResponse::Conflict()
// .json(serde_json::json!({"status": "fail","message": "Email already exist"}));
// }
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)
}
I am getting this error which is not making any sense at all because it works fine in the docs and all the examples I have seen. I googled the error but I found nothing. I tried ChatGPT but still the same error.
error[E0277]: the trait bound `i32: QueryResult<_>` is not satisfied
--> srcappusersviews.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:UsersAyush.cargoregistrysrcindex.crates.io-6f17d22bba15001fsurrealdb-1.4.2srcapimethodquery.rs:344:40
|
344 | pub fn take<R>(&mut self, index: impl opt::QueryResult<R>) -> Result<R>
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `Response::take`
I googled the error, looked at the docs and some examples and I found no difference in the implementation. I asked ChatGPT and it gave me the same example.