You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following the instructions here I was able to create a durable object, and this is reflected on my cloudflare dashboard but I cant seem to figure out how to access it.
This is my code:
use gloo_utils::format::JsValueSerdeExt;use serde::{Deserialize,Serialize};use serde_json::Value;use wasm_bindgen::JsValue;use worker::{durable_object,*};#[durable_object]pubstructStoreDurableObject{state:State,amount:i64,env:Env,}#[event(fetch)]asyncfnmain(req:Request,env:Env,_ctx:Context) -> Result<Response>{let router = Router::new();
router
.on_async("/durable", |req, ctx| asyncmove{durable_object_handler(req, ctx).await}).run(req, env).await}asyncfndurable_object_handler(mutreq:Request,ctx:RouteContext<()>) -> Result<Response>{let body = match req.clone().unwrap().json::<StoreObject>().await{Ok(res) => res,Err(e) => returnResponse::error(format!("Failed to parse JSON: {}", e),400),};let store_object = match ctx.durable_object("STORE"){Ok(res) => res,Err(e) => {returnResponse::error(format!("Failed to get Durable Object Namespace: {}", e),400,)}};console_log!("Store Object: {:?}", store_object.as_ref());let id = store_object.id_from_name(&body.store_id).unwrap();console_log!("id: {:?}", id.to_string());let stub = id.get_stub();// Get the request body as a Stringlet body = match req.json::<Value>().await{Ok(json) => JsValue::from_serde(&json)?,Err(err) => returnResponse::error(format!("Something went wrong: {err:?}"),500),};console_log!("DATA: {:#?}", body);// Create a new requestlet new_req = Request::new_with_init("/dobj",RequestInit::new().with_method(req.method()).with_body(Some(body)).with_headers(req.headers().to_owned()),)?;console_log!("REQUEST: {:?}", new_req);match stub.unwrap().fetch_with_request(new_req).await{Ok(res) => Response::ok(format!("Response: {:?}", res)),Err(err) => Response::error(format!("Err: {err:?}"),500),}}#[durable_object]implDurableObjectforStoreDurableObject{fnnew(state:State,env:Env) -> Self{Self{
state,amount:0,
env,}}asyncfnfetch(&mutself,req:Request) -> Result<Response>{let env = self.env.clone().into();let router = Router::with_data(self);
router
.post_async("/dobj", |mut req, ctx| asyncmove{letmut _self = ctx.data.borrow_mut();letmut storage = _self.state.storage();ifletOk(body) = req.json::<StoreObject>().await{
_self.amount = body.amount;
storage.put("amount", _self.amount).await?;Response::ok("Amount Inserted Into Durable Object")}else{Response::error("Bad Request",400)}}).run(req, env).await}}
Is there an existing issue for this?
What version of
workers-rs
are you using?0.3.1
What version of
wrangler
are you using?3.68.0
Describe the bug
Following the instructions here I was able to create a durable object, and this is reflected on my cloudflare dashboard but I cant seem to figure out how to access it.
This is my code:
And this is my curl request (from postman):
And this is the response (as text) I get back
I'm not sure what I am doing wrong. Any help would be appreciated.
Steps To Reproduce
No response
The text was updated successfully, but these errors were encountered: