0

When I try to fetch ... , I get an error. The url and port are correct, when I open the url in browser it works fine and gives me response, but when I try to connect to it from API route all breaks.

Error itself

TypeError: fetch failed

at Object.fetch (node:internal/deps/undici/undici:14062:11)

at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

{ cause: Error: connect ECONNREFUSED ::1:7071

at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) at

TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {

errno: -4078,

code: 'ECONNREFUSED',

syscall: 'connect',

address: '::1',

port: 7071 } }

API route

export async function GET(request: Request) {
    try {
        // Connect to mcrft azure func endpoint
        const response = await fetch(
            `${process.env.VERCEL_URL || "http://localhost:7071"
            }/api/getChatGPTSuggestion`,
            {
                cache: "no-store",
            }
        );

        const textData = await response.text();

        return new Response(JSON.stringify(textData.trim()), {
            status: 200,
        });
    } catch (error) {
        console.log("error inside get route", error)
        if (error instanceof Error) {
            return new Response(error.message, { status: 500 });
        }

        return new Response("Internal Server Error", { status: 500 });
    }
}

Cloud function

const { app } = require('@azure/functions')
const openai = require('../../lib/openai')

app.http('getChatGPTSuggestion', {
  methods: ['GET'],
  authLevel: 'anonymous',
  handler: async (request, context) => {
    const response = await openai.createCompletion({
      model: 'text-davinci-003',
      prompt:
        '...',
      max_tokens: 100, 
      temperature: 0.8,
    })

    context.log(`Http function processed request for url "${request.url}"`)

    const responseText = response.data.choices[0].text

    return {
      body: responseText,
    }
  },
})

1 Answer 1

0

The address: '::1' in your error message indicates it's trying to connect to localhost.

The ipv6 localhost address is 0000:0000:0000:0000:0000:0000:0000:0001 , which can be abbreviated to ::1

process.env.VERCEL_URL is probably not set correctly.

Sign up to request clarification or add additional context in comments.

3 Comments

process.env.VERCEL_URL is a system var which will be not undefined when I deploy my project, but now I connect only to localhost, it is like it needs to work, but I don't really undestand why I can't connect to localhost.
Are you facing the error locally or on the hosted environment?
I face the error locally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.