access Durable Object Id from within the DO

huh so this works great when I create the durable object. But breaks within the do For example this works
const id = c.env.USER_DO.idFromName("some_user_id");

// returns "some_user_id"
console.log('nameFromId', id.name);

const obj = c.env.USER_DO.get(id);
const id = c.env.USER_DO.idFromName("some_user_id");

// returns "some_user_id"
console.log('nameFromId', id.name);

const obj = c.env.USER_DO.get(id);
However within the durable object itself whjen I run id.name it returns undefined.
export class UserDurableObject extends DurableObject {
env: WorkerEnv;
state: DurableObjectState;
userId: string;
constructor(ctx: DurableObjectState, env: WorkerEnv) {
super(ctx, env);
this.env = env;
this.state = ctx;
const durableObjectId = this.state.id;

const userIdFromDo = durableObjectId.name;

// Errors because within the DO the name is not defined?
if (!userIdFromDo ) {
throw Error("doName is undefined");
}

this.userId = userIdFromDo;
}
}
export class UserDurableObject extends DurableObject {
env: WorkerEnv;
state: DurableObjectState;
userId: string;
constructor(ctx: DurableObjectState, env: WorkerEnv) {
super(ctx, env);
this.env = env;
this.state = ctx;
const durableObjectId = this.state.id;

const userIdFromDo = durableObjectId.name;

// Errors because within the DO the name is not defined?
if (!userIdFromDo ) {
throw Error("doName is undefined");
}

this.userId = userIdFromDo;
}
}
1 Reply
lucidplot
lucidplot2mo ago
ah ok I hadn’t seen that bug thread which HardlyWorkin referenced. I think you can access the ID though, just not the name it was derived from.
Want results from more Discord servers?
Add your server