Actix Web: Requested application data is not configured correctly. View/enable debug logs for more details

Viewed 376

I have a simple application with an HTTP endpoint and a connection to a MongoDB database.

use actix_web::{
    middleware, post,
    web::{self},
    App, HttpServer, Responder,
};
use mongodb::{options::ClientOptions, Client};
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct TestBody {
    name: String,
    age: u8,
}

#[post("/test")]
async fn test(query: web::Json<TestBody>, db: web::Data<Client>) -> impl Responder {
    for db_name in db.list_database_names(None, None).await.unwrap() {
        println!("{}", db_name);
    }

    let res = format!("{} {}", query.name, query.age);
    res
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let connection_string = "secret-connection-string";
    let client_options = ClientOptions::parse(connection_string).await.unwrap();
    let client = Client::with_options(client_options).unwrap();

    HttpServer::new(move || {
        App::new()
            .wrap(middleware::Compress::default())
            .app_data(client.clone())
            .app_data(web::JsonConfig::default())
            .service(test)
    })
    .bind("0.0.0.0:7080")?
    .run()
    .await
}

It compiles and runs just fine. But when trying to access localhost:7080/test, I get the following response:

Requested application data is not configured correctly. View/enable debug logs for more details.

I don't see any logs in the console. How do I view or enable the Actix Web logs?

1 Answers

I managed to see the logs by adding the dependencies of env_logger and log to the cargo.toml.

[dependencies]
env_logger = "0.9.0"
log = "0.4.17"

In the main function, I also had to set two environment variables and initialize the env_logger.

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    env_logger::init();

    /* ... */
}

This enables debug logging for Rust and Actix Web.


To solve the original issue: You always need to wrap data passed to app_data() with Data::new().

This is how I did it before:

HttpServer::new(move || {
  App::new()
    /* ... */
    .app_data(client.clone())
    /* ... */
})

How it should be instead:

HttpServer::new(move || {
  App::new()
    /* ... */
    .app_data(Data::new(client.clone())) // <-- Data::new() here
    /* ... */
})
Related