S
SolidJS3mo ago
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>
);
}
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
}
}
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?
4 Replies
Madaxen86
Madaxen863mo ago
Hm, probably the fetch never resolves? What happens when you comment out the fetch?
Nicolas
NicolasOP3mo ago
Hi! Thanks for your help with this. correct, when i comment out the fetch it works just fine, but as soon as i do any kind of fetch, it doesn't work and seems to fail silently or never resolve.
Madaxen86
Madaxen863mo ago
What node version are you using? Fetch is native in node from version 18. Maybe you need to install node-fetch? Hard to tell without code,..
Nicolas
NicolasOP3mo ago
I was able to fix it, hidden error with the proxy I had to do the following before i fetched with my proxy:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
process.emitWarning = () => {};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
process.emitWarning = () => {};
Want results from more Discord servers?
Add your server