Actix Web 4 始终添加 "Ok" 字段
Actix Web 4 always add "Ok" field
目前,我将 actix_web 从版本 3 更改为 4.0.0-rc.1,并且对于每个响应,它总是添加如下所示的“Ok”/“Err”字段
{
"Ok": [
"item1",
"item2"
]
}
应该return:
[
"item1",
"item2"
]
这是 API
的处理程序
pub async fn get_data(db: web::Data<Pool>) -> HttpResponse {
let res = web::block(move || db_get_data(db)).await;
match res {
Ok(data_vec) => HttpResponse::Ok().json(data_vec),
Err(_) => HttpResponse::BadRequest().finish()
}
}
fn db_get_data(db: web::Data<Pool>) -> Result<Vec<String>, ()> {
let items = vec!["item1".to_string(), "item2".to_string()];
Ok(items)
}
我该如何解决这个问题?
Result
有两层:一层来自actix_web::web::block
,另一层来自db_get_data
。尝试这样的事情:
let res = web::block(move || db_get_data(db)).await;
match res {
Ok(Ok(data_vec)) => HttpResponse::Ok().json(data_vec),
_ => HttpResponse::BadRequest().finish(),
}
目前,我将 actix_web 从版本 3 更改为 4.0.0-rc.1,并且对于每个响应,它总是添加如下所示的“Ok”/“Err”字段
{
"Ok": [
"item1",
"item2"
]
}
应该return:
[
"item1",
"item2"
]
这是 API
的处理程序pub async fn get_data(db: web::Data<Pool>) -> HttpResponse {
let res = web::block(move || db_get_data(db)).await;
match res {
Ok(data_vec) => HttpResponse::Ok().json(data_vec),
Err(_) => HttpResponse::BadRequest().finish()
}
}
fn db_get_data(db: web::Data<Pool>) -> Result<Vec<String>, ()> {
let items = vec!["item1".to_string(), "item2".to_string()];
Ok(items)
}
我该如何解决这个问题?
Result
有两层:一层来自actix_web::web::block
,另一层来自db_get_data
。尝试这样的事情:
let res = web::block(move || db_get_data(db)).await;
match res {
Ok(Ok(data_vec)) => HttpResponse::Ok().json(data_vec),
_ => HttpResponse::BadRequest().finish(),
}