#the `?` operator cannot be applied to type `modelsCreateUsers`

40 messages ยท Page 1 of 1 (latest)

cloud temple
#

this is the error I'm getting from the following code:

error[E0277]: the `?` operator can only be applied to values that implement `Try`
  --> src/users/db.rs:24:15
   |
24 |       let res = client
   |  _______________^
25 | |         .query(
26 | |             &statement,
27 | |             &[
...  |
37 | |         .map(|row| CreateUsers::from_row_ref(row).unwrap())
38 | |         .unwrap()?;
   | |__________________^ the `?` operator cannot be applied to type `models::CreateUsers`

What should I use instead of ?


pub async fn users_add(client: &Client, selfobj: CreateUsers) -> Result<CreateUsers, ServiceError> {
    let statement = client
        .prepare( "INSERT INTO public.users ( username, password, email)   VALUES ($1, $2, $3) RETURNING id, username, password, email",
        )
        .await
        .unwrap();

    let res = client
        .query(
            &statement,
            &[&selfobj.username, &selfobj.password, &selfobj.email, ],
        )
        .await
        .expect("Error creating users")
        .last()
        .map(|row| CreateUsers::from_row_ref(row).unwrap())
        .unwrap()?;
     match res {
        Ok(object) => object,
        Err(e) => Err(ServiceError::DuplicateValue(e)), // Err(e) => HttpResponse::InternalServerError().json(e.to_string())
    }

}

woeful phoenix
#

? is meant to be used instead of unwrap, not after it: It propagates the error to the caller, but for that to make sense it has to be used on a type which could be an error in the first place

cloud temple
#

ok

cloud temple
# woeful phoenix `?` is meant to be used _instead_ of `unwrap`, not _after_ it: It propagates the...

I'm faced with another error


error[E0308]: mismatched types
  --> src/users/db.rs:50:9
   |
49 |     match res {
   |           --- this expression has type `std::option::Option<models::CreateUsers>`
50 |         Ok(object) => object,
   |         ^^^^^^^^^^ expected enum `std::option::Option`, found enum `Result`
   |
   = note: expected enum `std::option::Option<models::CreateUsers>`
              found enum `Result<_, _>`

error[E0308]: mismatched types
  --> src/users/db.rs:51:9
   |
49 |     match res {
   |           --- this expression has type `std::option::Option<models::CreateUsers>`
50 |         Ok(object) => object,
51 |         Err(e) => ServiceError::DuplicateValue(e), // Err(e) => HttpResponse::InternalServerError().json(e.to_string())
   |         ^^^^^^ expected enum `std::option::Option`, found enum `Result`
   |
   = note: expected enum `std::option::Option<models::CreateUsers>`
              found enum `Result<_, _>`

error[E0308]: `match` arms have incompatible types
  --> src/users/db.rs:51:19
   |
49 | /     match res {
50 | |         Ok(object) => object,
   | |                       ------ this is found to be of type `Result<models::CreateUsers, ServiceError>`
51 | |         Err(e) => ServiceError::DuplicateValue(e), // Err(e) => HttpResponse::InternalServerError().json(e.to_string())
   | |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found enum `ServiceError`
52 | |     }
   | |_____- `match` arms have incompatible types
   |
   = note: expected enum `Result<models::CreateUsers, ServiceError>`
              found enum `ServiceError`
help: try wrapping the expression in `Err`

#

this is kind of confusing, i wasn't expecting an option

woeful phoenix
#

The last error in this list seems different, but it may also be a confused compiler, not sure

cloud temple
#

the return value is a result, which I wanted to propagate

woeful phoenix
#

My theory is that map is returning a Result<Option<CreateUsers>, ServiceError>

cloud temple
#

no, i removed it because of an error

#

error[E0277]: the ? operator can only be used on Results, not Options, in an async function that returns Result
--> src/users/db.rs:37:60
|

woeful phoenix
#

Well, then it appears you have an Option and not a Result

#

I'm not sure what to tell you. You say you think it's a Result, but it clearly isn't

#

Maybe the Result is returned by the await earlier?

#

The one you expect away?

#

Maybe you want to ? there? idk

woeful phoenix
#

Then you wait to ? that, not the final value of the chain

cloud temple
#

modifying the code as suggested returns the same issue, how do i handle the option and have Result as a return value ?

woeful phoenix
cloud temple
#

ok, let me see

#

it looks promising, thanks a lot

#

but how do i send the error value to Err?

woeful phoenix
#

If it's an Option, there is no error value

#

None doesn't contain a value

cloud temple
#

that's why I wanted to instead pass a Result so I can catch the error

woeful phoenix
#

If a function decided to return an Option, it returns an Option

#

You don't get to tell it "actually return a result instead"

cloud temple
#

oh ๐Ÿ˜„

woeful phoenix
#

It's not that there was an error and the function has thrown it away for you

#

Usually, it's that there wasn't an error in the first place

#

There was only None

cloud temple
#

what do you suggest?

woeful phoenix
#

And the last() of zero elements just doesn't exist

#

hence the Option