I'm learning typescript and messing around with an API endpoint, I've created a method which takes a value, makes a request to an external API and gets the data, populates the model PortalDetail and returns it back to the front end.
However, the returned data is always null. I have checked the portalUrl
and the value exists, so I'm not sure what I'm doing wrong here:
Here is the code in question:
const CheckUri = async (organisationCode: string): Promise<PortalDetail | null> => {
await fetch(`${uris.appCode}${organisationCode}`)
.then(async (data) => {
const portalUrl = await data
.text()
.then((html) => html)
.catch(() => null)
console.log(portalUrl); // This has a value
console.log(data); // So does this
if (data?.status === 200 && portalUrl) {
console.log("inside if"); // This also gets printed
return new PortalDetail(portalUrl, organisationCode);
}
})
.catch(() => {
})
.finally(() => {
})
return null;
}
Here is the code calling the method:
const CheckOrgnisationCode = async (orgCode: string) => {
if (!orgCode || orgCode.includes(' ')) {
return;
}
console.log("before checkuri method")
var portalDetails = await CheckUri(orgCode);
console.log("finiahes ");
console.log(portalDetails);
if (portalDetails?.portalUrl) {
await StoreData(saved.portalURL, portalDetails?.portalUrl);
await StoreData(saved.portalCode, orgCode);
}
}
Can someone please explain where I'm going wrong?