kwhitley / itty-durable

Cloudflare Durable Objects + Itty Router = shorter code
MIT License
251 stars 18 forks source link

Question. Values from durable object #40

Closed nikitapilgrim closed 1 year ago

nikitapilgrim commented 1 year ago

I can't access the values from the durable object. They become visible only in the response

You have such an example .get('/', ({ Counter }) => Counter.get('test').toJSON())

I have something like this .get('/', async ({ Counter }) => { const state = await Counter.get('test').toJSON()); console.log(JSON.stringify(state), state); return json(state); }

the response in the cloudflare worker dashboard.


 "message": [
        "{}",
        "[object Object]"
      ],

but the response in the browser is fine, I can see all the values of the object.

kwhitley commented 1 year ago

So if you don’t use the parse flag (autoParse?) on the withDurables middleware, the response back from a DO method (e.g. toJSON) will be a Response, not the raw data. So you wouldn’t want to stringify it. Your final json method is ignoring it because it’s already a valid Response, so it still return just fine in the end.~KevinOn Jul 23, 2023, at 00:46, Nikita Pilgrim @.***> wrote: I can't access the values from the durable object. They become visible only in the response You have such an example .get('/', ({ Counter }) => Counter.get('test').toJSON()) I have something like this .get('/', async ({ Counter }) => { const state = await Counter.get('test').toJSON()); console.log(JSON.stringify(state), state); return json(state); } the response in the cloudflare worker dashboard.

"message": [ "{}", "[object Object]" ],

but the response in the browser is fine, I can see all the values of the object.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>