0

I updated nuxtjs application version 3.2.0 to 3.4.3. now when a client call server api (which calling a backend server) i have this exception

[nitro] [dev] [uncaughtException] TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Response 3:13:26 PM at new NodeError (node:internal/errors:372:5) at write_ (node:_http_outgoing:742:11) at ServerResponse.end (node:_http_outgoing:855:5) at Immediate. (file:///home/xxx/dev/projects/websites.modules/ae.loftoffices/node_modules/h3/dist/index.mjs:613:22) at processImmediate (node:internal/timers:466:21) { code: 'ERR_INVALID_ARG_TYPE' }

i dont understand what is the issue, here the api

export default defineEventHandler(async (event) => {
    const {BACKEND_REST_API, ENQUIRY_TOKEN, ENQUIRY_PERSON_IN_CHARGE_EMAIL, COMPANY_UNID, ENQUIRY_TITLE, ENQUIRY_TYPE, ENQUIRY_SOURCE, } = useRuntimeConfig();
    const form = await readBody(event);
    console.log("url used for enquiry rest call :" + BACKEND_REST_API);
    console.log("Enquiry token :" + ENQUIRY_TOKEN);

    form.author = ENQUIRY_TITLE
    form.personInChargeEmail = ENQUIRY_PERSON_IN_CHARGE_EMAIL;
    form.contactPersonCompany = 'N/A';
    form.companyUnid = COMPANY_UNID;
    form.type = ENQUIRY_TYPE;
    form.source = ENQUIRY_SOURCE;
    delete form.bot;

    console.log(form);
    return await $fetch.raw(BACKEND_REST_API+"/enquiry/add", {
        method: "POST",
        body: form,
        headers: {
            Authorization: ENQUIRY_TOKEN,
        },
    })
})

and the client call

await useFetch("/api/enquiry", {
        method: "POST",
        body: values,
        onResponse({request, response, options}) {
            // Process the response data
            if (response.status === 200) {
                errorMessage.value = "";
                successMessage.value = "Your message was sent successfully and will receive further emails from us soon. Thank you!";
            }
        },
        onResponseError({request, response, options}) {
            console.debug(response);
            if (response.status === 400) {
                successMessage.value = "";
                errorMessage.value = "There may be an issue with our server. Please try again later, or send an email to info@loftoffices.ae";
            } else {
                successMessage.value = "";
                errorMessage.value = "Sorry we couldn’t send the message, there may be an issue with our server. Please try again later, or send an email to ........"
            }
        },
    });

any idea?

cyril
  • 872
  • 6
  • 29

2 Answers2

0

so the issue was we tried to parse a Response type (come from jakarta rs java)

so we changed

return await $fetch.raw(BACKEND_REST_API+"/enquiry/add", {

to

 return await $fetch(BACKEND_REST_API+"/enquiry/add", {
cyril
  • 872
  • 6
  • 29
  • By doing so you'll lose access to cookies which still problematic. – ALOUI MH Jun 12 '23 at 10:31
  • for now i dont care about cookie, maybe if i need later i will take care, but it is a workaround instead stay stucked for whole days ^^ – cyril Jun 14 '23 at 06:35
0

To maintain the use of $fetch.raw and access cookies, you must return a valid object that meets the requirements of useFetch used in the front end. The /server/api/ENDPOINT should return the following object:

return {
    _data: data._data 
}
ALOUI MH
  • 97
  • 1
  • 10
  • it was working well before migration with useFetch, and sorry but here your code is not complete it can be interpreted as we want :/, thank you anyway – cyril Jun 19 '23 at 12:57