简体   繁体   中英

How to use rust diesel with yew or any frontend?

I build a rust database with diesel just like the docs and it worked fine in the terminal commands



fn main() {
    use database::schema::posts::dsl::*;

    let connection = establish_connection();
    let results = posts.filter(published.eq(true))
        .limit(5)
        .load::<Post>(&connection)
        .expect("Error loading posts");

    println!("Displaying {} posts", results.len());
    for post in results {
        println!("{}", post.title);
        println!("----------\n");
        println!("{}", post.body);
    }
}


but later when I used the following function inside yew I got the error


pub fn get_posts() -> Vec<Post> {
    let connection = establish_connection();
    use schema::posts::dsl::*;
    let results = posts.load::<Post>(&connection).expect("Error loading posts");
    results
}
Uncaught TypeError: Failed to resolve module specifier "env". Relative references must start with either "/", "./", or "../".
  1. I am using postgres app on mac
  2. i am using DATABASE_URL=postgres://apple:password@localhost/postgres for postgresql connect

You should probably have a backend server that has an access to the database, and your yew frontend calls that backend.

See for instance: https://github.com/tokio-rs/axum/blob/main/examples/sqlx-postgres/src/main.rs

If you prefer to use diesel, you would of course have to to adapt this example to make it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM