gpt4 book ai didi

mongodb - 将 mongodb 2 与 actix-web 3 一起使用时为 "there is no reactor running, must be called from the context of a Tokio 1.x runtime"

转载 作者:行者123 更新时间:2023-12-05 02:40:30 25 4
gpt4 key购买 nike

我尝试用 actix web 实现 mongodb。这是我的主课:

#[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"

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com