Compiling infraestructure v0.1.0 (/my-api/infraestructure)
error: comparison operators cannot be chained
--> infraestructure/src/db.rs:45:45
|
45 | row.get("description").Optio<T>
| ^ ^
|
= help: use `::<...>` instead of `<...>` to specify type arguments
= help: or use `(...)` if you meant to specify fn arguments
error: aborting due to previous error
That “.” was the end of @sfackler's sentence, not part of any rust syntax. The next sentence starts with “Option<T>”.
As far as I understand, you just need to remove the Some() around the expression, turning Some(row.get("description")) into row.get("description"), to make it work.
So it might have helped people help you if you showed the JSON serialization code that failed rather than something else.
I have potentially NULL fields in my database. The way to deal with it is to have Option fields in the structure that you are fetching rows into. My code looks like this:
#[derive(Serialize)]
struct Person {
id: i64,
name: String,
data: Option<Vec<u8>>,
}
#[derive(Serialize)]
struct Persons {
persons: Vec<Person>,
}
#[get("/public/api/persons")]
fn api_persons(model: State<Model>) -> Json<Persons> {
let _current_count = model.count.fetch_add(1, Ordering::Relaxed);
let mut conn = model.connect();
let rows = conn
.query("SELECT id, name, data FROM person", &[])
.unwrap();
let mut results = Persons { persons: vec![] };
for row in &rows {
let person = Person {
id: row.get("id"),
name: row.get("name"),
data: row.get("data"),
};
results.persons.push(person);
}
Json(results)
}