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);


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;
  }
}
Was this page helpful?