API resolved without sending a response for /api/geolocation-api, this may result in stalled request
Unanswered
Kuchi posted this in #help-forum
KuchiOP
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "../../lib/session";
export default withIronSessionApiRoute(GeoLocate, sessionOptions);
async function GeoLocate(req, res) {
const apiKey = process.env.GEOLOCATION_API_KEY;
let ip = "";
if (req.headers["x-forwarded-for"]) {
// When not running on localhost
ip = req.headers["x-forwarded-for"];
} else if (req.socket.remoteAddress && req.socket.remoteAddress !== "::1") {
// When running on localhost with IP other than '::1'
ip = "142.31.216.15";
}
console.log("ip", ip);
const url = `https://geo.ipify.org/api/v1?apiKey=${apiKey}&ipAddress=${ip}`;
var location = "";
try {
const response = await fetch(url);
if (response.ok) {
const data = await response.json();
console.log("data", data);
location = data.location.country;
} else {
throw new Error("Failed to fetch geolocation");
}
} catch (error) {
console.error("Error fetching geolocation:", error);
}
return location;
}How do I send the response?