Skip to content

Rust build error inside function list_dogs from "https://dioxuslabs.com/learn/0.7/tutorial/routing" #641

@Bohemian2024

Description

@Bohemian2024

The following code can't pass Rust build,

// Query the database and return the last 10 dogs and their url
#[server]
pub async fn list_dogs() -> Result<Vec<(usize, String)>, ServerFnError> {
    let dogs = DB.with(|f| {
        f.prepare("SELECT id, url FROM dogs ORDER BY id DESC LIMIT 10")
            .unwrap()
            .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
            .unwrap()
            .map(|r| r.unwrap())
            .collect()
    });

    Ok(dogs)
}

The error is:
the trait bound usize: FromSql is not satisfied
the following other types implement trait FromSql:
f32
f64
i16
i32
i64
i8
isize
u16
and 2 others

Fix proposal:
Change usize to isize in function declaration.

#[server]
pub async fn list_dogs() -> Result<Vec<(isize, String)>, ServerFnError> {
    let dogs = DB.with(|f| {
        f.prepare("SELECT id, url FROM dogs ORDER BY id DESC LIMIT 10")
            .unwrap()
            .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
            .unwrap()
            .map(|r| r.unwrap())
            .collect()
    });

    Ok(dogs)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions