Durable object resets value on each request to Worker

According to the docs I've created an durable object:
[[durable_objects.bindings]]
name = "COUNTERS"
class_name = "UserIdCounter"

[[migrations]]
tag = "v1"
new_classes = ["UserIdCounter"]
[[durable_objects.bindings]]
name = "COUNTERS"
class_name = "UserIdCounter"

[[migrations]]
tag = "v1"
new_classes = ["UserIdCounter"]
With proper class in my Worker:
export class UserIdCounter extends DurableObject {
async increment(amount = 1) {
let value = (await this.ctx.storage.get("value")) || 0;
value += amount;
await this.ctx.storage.put("value", value);
return value;
}
}

export default {
fetch: router.handle // my worker logic
}
export class UserIdCounter extends DurableObject {
async increment(amount = 1) {
let value = (await this.ctx.storage.get("value")) || 0;
value += amount;
await this.ctx.storage.put("value", value);
return value;
}
}

export default {
fetch: router.handle // my worker logic
}
When I'm trying to increment value, I'm always getting 1 for some reason:
const counterStub = env.COUNTERS.get(env.COUNTERS.idFromName('website'));
await counterStub.increment();
const counterStub = env.COUNTERS.get(env.COUNTERS.idFromName('website'));
await counterStub.increment();
Using latest version of. Wrangler with command: wrangler dev --remote --local-protocol https
No description
3 Replies
satanch
satanchOP2mo ago
The sad thing that I need to create another worker to test it when deployed. So I'm gonna create another worker to test deployed Durable object counter. Btw when testing locally with --remote arg I'm unable to see Durable object at the dashboard (see image).
No description
satanch
satanchOP2mo ago
name = "***-wrangler"
main = "index.js"
compatibility_date = "2024-04-03"
kv_namespaces = [
{ binding = "SMVFBIND", id = "***", preview_id = "***" }
]

[[durable_objects.bindings]]
name = "COUNTERS"
class_name = "UserIdCounter"

[[migrations]]
tag = "v1"
new_classes = ["UserIdCounter"]
name = "***-wrangler"
main = "index.js"
compatibility_date = "2024-04-03"
kv_namespaces = [
{ binding = "SMVFBIND", id = "***", preview_id = "***" }
]

[[durable_objects.bindings]]
name = "COUNTERS"
class_name = "UserIdCounter"

[[migrations]]
tag = "v1"
new_classes = ["UserIdCounter"]
full wrangler.toml, maybe smth wrong with compatibility_date?
kchro3
kchro32mo ago
^it would be great if that could be documented somewhere. I didn’t know that remote dev would only create a temporary version for DOs. Also, if they aren’t in the dashboard at all, it’s hard to debug in dev
Want results from more Discord servers?
Add your server