Node-fetch compatibility

v4

#1

Hi, I’m using node-fetch (v2) to make backend requests. Using React Web Client & Jovo 4.

Ex:

const ip = await fetch('https://httpbin.org/ip');
this.$send(`Your IP is: ${ip}`);

But once fetch is executed, Jovo is returning response even before the $send() is called and no output is received to the web client. But I can see output in Jovo Debugger.

Is this a known issue? Or, am I missing something? Thanks


#2

You should await response.json().

Something like this will work

  type HttpbinIpResponse = {
    origin: string;
  };

  Webhook.get('/ip', async (req: Request, res: Response) => {
    const response = await fetch('https://httpbin.org/ip');
    const body = (await response.json()) as HttpbinIpResponse;
    res.json({ ip: body.origin });
  });

#3

To make is simple, I simplified the example code, but now I see that I over did it.

The moment await fetch() got executed, Jovo is sending output. In below pictures, execution is waiting on breakpoint just after await fetch() and I can see output already in Jovo Debugger.

Below is the actual code I have:

So, I’m pretty sure it is not the await resp.json(). Thanks


#4

My bad. Problem is a missing await for at higher level when invoking the service in recursive manner.


#5

Been there, done that…