首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB与r2d2和actix在锈病中的应用

MongoDB与r2d2和actix在锈病中的应用
EN

Stack Overflow用户
提问于 2019-08-06 08:51:27
回答 1查看 1.4K关注 0票数 2

我试图使用锈迹语言开发一个基本的web应用程序,使用actix框架和以mongodb作为数据库的r2d2。我找不到任何关于如何存档的完整和工作文档。也许有人能帮我。

问题是,我似乎无法从r2d2连接池获得mongodb连接。遗憾的是,我发现的任何文档中都没有涵盖这部分内容。

我发现了一些链接:

此部分创建连接池并将其交给actix。

代码语言:javascript
复制
fn main() {
    std::env::set_var("RUST_LOG", "actix_web=info");
    env_logger::init();

    let manager = MongodbConnectionManager::new(
        ConnectionOptions::builder()
            .with_host("localhost", 27017)
            .with_db("mydatabase")
            .build()
    );    

    let pool = Pool::builder()
        .max_size(16)
        .build(manager)
        .unwrap();

    HttpServer::new( move || {
        App::new()
            // enable logger
            .wrap(middleware::Logger::default())
            // store db pool in app state
            .data(pool.clone())
            // register simple handler, handle all methods
            .route("/view/{id}", web::get().to(view))
    })
    .bind("127.0.0.1:8080")
    .expect("Can not bind to port 8080")
    .run()
    .unwrap();
}

这是试图访问连接池的处理程序函数。

代码语言:javascript
复制
fn view(req: HttpRequest, 
        pool: web::Data<Pool<MongodbConnectionManager>>) -> impl Responder {

    let id = req.match_info().get("id").unwrap_or("unknown");
    let conn = pool.get().unwrap();
    let result = conn.collections("content").findOne(None, None).unwrap();

   // HERE BE CODE ...

    format!("Requested id: {}", &id)
}

这是显示我问题的错误。conn变量似乎不是propper连接。

代码语言:javascript
复制
error[E0599]: no method named `collections` found for type `std::result::Result<r2d2::PooledConnection<r2d2_mongodb::MongodbConnectionManager>, r2d2::Error>` in the current scope  --> src\main.rs:29:23
   |
29 |     let result = conn.collections("content").findOne(None, None).unwrap();
   |   
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-31 16:31:36

代码语言:javascript
复制
10 |     let coll = conn.collection("simulations");
   |                     ^^^^^^^^^^
   |
   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope, perhaps add a `use` for it:
           `use crate::mongodb::db::ThreadedDatabase;`

我的编译器让我在作用域中添加mongodb::db::ThreadedDatabase

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57372294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档