Coder Social home page Coder Social logo

Comments (5)

fundon avatar fundon commented on August 20, 2024

Do you have the complete code?

from viz.

Baiyuetribe avatar Baiyuetribe commented on August 20, 2024

async fn show(mut req: Request) -> Result<Response> {

modify this method:

async fn show(_: Request) -> Result<Response> {
    let mut req = Request::builder().body(IncomingBody::Empty).unwrap();
    let State(db) = req.extract::<State<DB>>().await?;
    let _ = db.lock();
    println!("print if db is ok");
    Ok(Response::text("Hello, World!"))
}

cargo run --bin todos
try : http://127.0.0.1:3000/todos/1

from viz.

fundon avatar fundon commented on August 20, 2024

@Baiyuetribe

It's correct. In original, the DB state has been added to the request:

.with(State::new(db))

from viz.

Baiyuetribe avatar Baiyuetribe commented on August 20, 2024

@fundon no,it not work

async fn show(mut req: Request) -> Result<Response> {

modify this method:

async fn show(_: Request) -> Result<Response> {
    let mut req = Request::builder().body(IncomingBody::Empty).unwrap();
    let State(db) = req.extract::<State<DB>>().await?; // ***erorr***
    let _ = db.lock();
    println!("print if db is ok");
    Ok(Response::text("Hello, World!"))
}

cargo run --bin todos try : http://127.0.0.1:3000/todos/1

from viz.

fundon avatar fundon commented on August 20, 2024

Forged a request, if we want to extract some stats from request, we should add stats to it use set_state method, when it cannot be found, a 500 error is returned.

viz/viz-core/src/request.rs

Lines 141 to 145 in 7fc08df

/// Store a shared state.
#[cfg(feature = "state")]
fn set_state<T>(&mut self, t: T) -> Option<T>
where
T: Clone + Send + Sync + 'static;

from viz.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.