"there is no reactor running, must be called from the context of a Tokio 1.x runtime" 将 mongodb 2 与 actix-web 3 一起使用时
"there is no reactor running, must be called from the context of a Tokio 1.x runtime" when using mongodb 2 with actix-web 3
我尝试使用 actix web 实现 mongodb。这是我的主要 class:
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let db_helper = DbHelper::open_connection().await?;
println!("Connected!");
Ok(())
}
// my db helper
pub async fn open_connection() -> std::io::Result<DbHelper> {
let client = Client::with_uri_str("mongodb://localhost:27017/").await.expect("connect to mongodb issue!");
// List the names of the databases in that deployment.
let databases = client.list_database_names(None, None).await.expect("Test error");
for mongodb in databases {
println!("{}", mongodb);
}
let database = client.database(DB_NAME);
let db_helper = DbHelper {
client,
database,
};
Ok(db_helper)
}
当我尝试 运行 这段代码时,它给了我一个错误:
thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime',
问题出在这一行:let databases = client.list_database_names(None, None).await.expect("Test error");
。我不知道这里发生了什么。如果我删除该行或将 actix_web::main
替换为 #[tokio::main]
,它就可以工作。但是,actix web无法运行。
actix-web = "3"
mongodb = "2.0.0-beta.2"
问题是您将 Actix web 3.x 与 Tokio 1.x 和 mongoDB 客户端 2.x 一起使用。您需要使用 Actix web 4.x 才能与其他两个一起使用。
我尝试使用 actix web 实现 mongodb。这是我的主要 class:
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let db_helper = DbHelper::open_connection().await?;
println!("Connected!");
Ok(())
}
// my db helper
pub async fn open_connection() -> std::io::Result<DbHelper> {
let client = Client::with_uri_str("mongodb://localhost:27017/").await.expect("connect to mongodb issue!");
// List the names of the databases in that deployment.
let databases = client.list_database_names(None, None).await.expect("Test error");
for mongodb in databases {
println!("{}", mongodb);
}
let database = client.database(DB_NAME);
let db_helper = DbHelper {
client,
database,
};
Ok(db_helper)
}
当我尝试 运行 这段代码时,它给了我一个错误:
thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime',
问题出在这一行:let databases = client.list_database_names(None, None).await.expect("Test error");
。我不知道这里发生了什么。如果我删除该行或将 actix_web::main
替换为 #[tokio::main]
,它就可以工作。但是,actix web无法运行。
actix-web = "3"
mongodb = "2.0.0-beta.2"
问题是您将 Actix web 3.x 与 Tokio 1.x 和 mongoDB 客户端 2.x 一起使用。您需要使用 Actix web 4.x 才能与其他两个一起使用。