can someone help me here? I have this general basic setup and am trying to do R2 operations within a

can someone help me here? I have this general basic setup and am trying to do R2 operations within a DO but , but i cannot figure out why the env bindings are not being recognized . I get
[ERROR] TypeError: Cannot read properties of undefined (reading 'MY_BUCKET')
. I can do R2 operations in the hono routes but not within the DO at the moment using the bindings

The use case is i need to upload a large CSV file so need to use R2 storage rather than DO storage

I am running this local.

type Bindings = {
    MY_BUCKET: R2Bucket
    MY_DO: DurableObjectNamespace<MyDurableObject>
}

type Variables = {}

export type Env = {
    Bindings: Bindings
    Variables: Variables
}


Durable Object Definition
export class MyDurableObject extends DurableObject<Env> {
    storage: DurableObjectStorage

    constructor(ctx: DurableObjectState, env: Env) {
        super(ctx, env)
        this.storage = ctx.storage
        console.log('DO constructor', this.env)
    }

    async test() {
        const upload = await this.env.Bindings.MY_BUCKET.put(`test`, 'test')
        return upload
    }
}


Calling the Durable Object in Hono
// do imported above

const id = c.env.MY_DO.idFromName('my-durable-object')
const durableObject = c.env.MY_DO.get(id)
const zipFile = await durableObject.test()
return c.json({ message: 'Test', zipFile })


export {MY_DO}


Logging and Error Output
DO constructor {
    MY_DO: DurableObjectNamespace {},
    MY_BUCKET: R2Bucket {},

  }
  ✘ [ERROR] TypeError: Cannot read properties of undefined (reading 'MY_BUCKET')
Was this page helpful?