#Service binding in dev

24 messages · Page 1 of 1 (latest)

lime dagger
#

I'm trying to get service bindings working in a dev environment, but keep just getting the following error:
Cannot read properties of undefined (reading 'fetch')

That's when I'm running wrangler dev --local in both the gateway worker and the simple fetch worker.

The code for the fetch worker is simple hello world.

export default {
    async fetch(
        request: Request,
        env: Env,
        ctx: ExecutionContext
    ): Promise<Response> {
        return new Response("Hello World!");
    },
};

And that worker is called counter and the service binding in the gateway is as follows:

services = [
  { binding = "counter", service = "counter", environment = "production" }
]

This originally didn't have the environment, but that didn't seem to make any difference.

When running wrangler without --local e.g. wrangler dev, it doesn't error, but the response come back empty.

Any ideas?

hushed hearth
#

You need both workers running with --local at the same time

lime dagger
#

Yep they were both running with --local

#

Also, when running them both in local, if I log env, I see just the DB connection I've setup on the gateway:
{ DB: BetaDatabase {} }

however when running them both with wrangler dev without the --local flag, I see the following output:

Object {
  counter: Fetcher,
  DB: D1Database
}
#

What sort of folder structure do I need for this to work locally? Do I need to somehow connect the two workers other than by the bindings? Do I need to import them or anything like that?

hushed hearth
#

Is your wrangler version up to date?

lime dagger
#

I think so, but I will check that now.

#

wrangler 2.6.2 (update available 2.9.1)

#

I'll update now and see if that fixes it.

lime dagger
#

Sadly still the same issue.
Whenever I try to access:
const response = await env.counter.fetch(request.clone()); from my gateway, I get the following message:
Cannot read properties of undefined (reading 'fetch')

#

Perhaps I'm missing something with my config file, as it seems to not be binding the worker.

This is the wrangler.toml for my api gateway:

name = "api-trpc"
main = "src/index.ts"
compatibility_date = "2023-01-29"

[[ d1_databases ]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "basic_cms"
database_id = "****"
preview_database_id = "****"

services = [
  { binding = "counter", service = "counter", environment = "production" }
]
hushed hearth
#

Can you move services above d1 databases? My guess is this is a toml being weird about tables thing

lime dagger
#

That seems to have been the issue!! Thanks!

#

I've still got a slight issue, in that the data's not coming across, or it's coming in a stream, but the connection's now working.

hushed hearth
#

Could you explain the new issue?

lime dagger
#

the response is coming out like so:

Response {
  [Symbol(realm)]: null,
  [Symbol(state)]: {
    aborted: false,
    rangeRequested: false,
    timingAllowPassed: true,
    requestIncludesCredentials: true,
    type: 'default',
    status: 200,
    timingInfo: {
      startTime: 2677.13970798254,
      redirectStartTime: 0,
      redirectEndTime: 0,
      postRedirectStartTime: 2677.13970798254,
      finalServiceWorkerStartTime: 0,
      finalNetworkResponseStartTime: 0,
      finalNetworkRequestStartTime: 0,
      endTime: 0,
      encodedBodySize: 14,
      decodedBodySize: 14,
      finalConnectionTimingInfo: null
    },
    cacheState: '',
    statusText: 'OK',
    headersList: HeadersList {
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    },
    urlList: [ [URL] ],
    body: { stream: undefined }
  },
  [Symbol(headers)]: HeadersList {
    [Symbol(headers map)]: Map(5) {
      'content-type' => 'application/json',
      'content-length' => '14',
      'date' => 'Sun, 12 Feb 2023 13:41:40 GMT',
      'connection' => 'keep-alive',
      'keep-alive' => 'timeout=5'
    },
    [Symbol(headers map sorted)]: null
  }
}
#

if I try to access response.body, I get ReadableStream { locked: false, state: 'readable', supportsBYOB: true }

#

Perhaps I'm just misunderstanding how I'm meant to work with the response.

hushed hearth
#

What do you want to read it as, text, JSON, binary, etc?

lime dagger
#

json or text

hushed hearth
#

await response.text()/.json() would be what you want then

#

And that's not specific to service bindings - it's how every fetch in workers & browsers work

lime dagger
#

That was it! Thanks! 👍

#

Thanks so much for all your help!!