我试图使用锈迹语言开发一个基本的web应用程序,使用actix框架和以mongodb作为数据库的r2d2。我找不到任何关于如何存档的完整和工作文档。也许有人能帮我。
问题是,我似乎无法从r2d2连接池获得mongodb连接。遗憾的是,我发现的任何文档中都没有涵盖这部分内容。
我发现了一些链接:
此部分创建连接池并将其交给actix。
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();
}这是试图访问连接池的处理程序函数。
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连接。
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();
| 发布于 2019-08-31 16:31:36
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。
https://stackoverflow.com/questions/57372294
复制相似问题