roku675
roku675

Reputation: 91

Error with tokio postgres and database query Rust

I am working on a rust project where I am working with tokio postgres. I have a table by name of DATA that has two columns "id" and "opt". I have the data hitting the query as two inputs where one is a String and the other is an Option.

Following tokio postges conventions, I am having to call the client with the help of the source code provided below.I am using COALESCE to check if second parameter is NULL on the sql query and this way I am making the SQL query compact.

let rows = client.query("SELECT * FROM DATA WHERE id = $1 AND opt = COALESCE(opt, $2)", &[&input1, &input2)]).await?;

I am getting this error when I perform a cargo build.

expected `&dyn ToSql + Sync`, found enum `Result`

Can this error be mitigated from Rust?

Upvotes: 0

Views: 637

Answers (1)

Marcus Ilgner
Marcus Ilgner

Reputation: 7241

The result from client.query is of type Result. Before working with the actual rows, you'll need to retrieve them from the Result type and deal with any errors that might have occurred.

See the Rust Book section or error handling for details on how to properly separate successful from erroneous results.

You can find the full function signature at docs.rs, for example, where you'll find that the return type is of Result<Vec<Row>, Error>.

Upvotes: 2

Related Questions