简体   繁体   English

如何在红豆杉或任何前端使用生锈柴油?

[英]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我像文档一样用柴油构建了一个 rust 数据库,它在终端命令中运行良好



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但后来当我在 yew 中使用以下函数时,我得到了错误


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我在 mac 上使用 postgres 应用程序
  2. i am using DATABASE_URL=postgres://apple:password@localhost/postgres for postgresql connect我正在使用DATABASE_URL=postgres://apple:password@localhost/postgres进行 postgresql 连接

You should probably have a backend server that has an access to the database, and your yew frontend calls that backend.您可能应该有一个可以访问数据库的后端服务器,并且您的 yew 前端调用该后端。

See for instance: https://github.com/tokio-rs/axum/blob/main/examples/sqlx-postgres/src/main.rs参见例如: 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.如果您更喜欢使用柴油,您当然必须修改此示例以使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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