SolidJSS
SolidJSโ€ข2y agoโ€ข
5 replies
Nicolas

Can't get fetch to work in server code

Hi all,

I have a simple form and action like so:
import { action } from "@solidjs/router";
import { checkEligibility } from "~/lib/proxy/proxy";

const myAction = action(async (formData: FormData) => {
  "use server";
  const address = String(formData.get("address"))
  const result = await checkEligibility(address);
  console.log(result);
})

export default function Verify() {
  return (
    <form action={myAction} method="post">
      <input
        type="text"
        name="address"
        placeholder="Enter Address"        
      />
      <button
        type="submit"
      >
        Verify
      </button>
    </form>
  );
}


And my function being called is like so:
export const checkEligibility = async (address: string) : Promise<z.infer<typeof checkResponse>> => {
  console.log("Checking eligibility", address);
  const brd_connectStr = "http://brd-customer-" + Resource.BrdUser.value + "-zone-" + Resource.BrdZone.value + ":" + Resource.BrdPassword.value + "@" + Resource.BrdSuperProxy.value;
  console.log(brd_connectStr);
  const baseUrl = "https://eligibility.sc.egov.usda.gov/eligibility/MapAddressVerification?"; 
  
  console.log("About to fetch..."); 
  const response = await fetch(baseUrl + new URLSearchParams({
    address: address,
    whichapp: "SHFPREV",    
  }).toString());

  console.log(response);
  // try to safely zod parse the response text
  const result = await eligiblityResult.safeParseAsync(response.text)

  console.log(result);

  return {
    isEligible: result.data?.eligibilityResult === "Eligible",
    message: result.error?.message
  }
}


But for some reason when i try to submit the form and run this code, it only gets to right before the
fetch
since it console logs About to fetch... but it never logs the response. There is no error being thrown, or anything at all. I checked the browser console and my server logs (showing About to fetch...), but nothing shows after that.

What could be going wrong?
Was this page helpful?