-1

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
Ali Husham
  • 816
  • 10
  • 31

1 Answers1

1

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.

Jeremy
  • 36
  • 3
  • I want to use a direct function call instead of api calls. Maybe something like wasm – Ali Husham Jun 19 '22 at 15:08
  • Wasm will be read by your browser, and it is not possible to call the database directly from your browser. An other possibility would be to generate the html from the server side and return it, with something like flask and Jinja (you can use tera for this purpose in rust). But I have no idea whether you'll be able to use any front end framework for this, or it would likely require more time than just creating a backend service. – Jeremy Jun 19 '22 at 19:40