I get the error mentioned in the title for the following typescript middleware: (target: es6, module commonjs)
try {
const response = await fetch(URL);
if (response.status !== 200) {
throw 'request not successful'
}
const data = await response.json();
const { keys } = data;
for (let i = 0; i < keys.length; i++) {
const key_id = keys[i].kid;
const modulus = keys[i].n;
const exponent = keys[i].e;
const key_type = keys[i].kty;
const jwk = { kty: key_type, n: modulus, e: exponent };
const pem = jwkToPem(jwk);
pems[key_id] = pem;
}
console.log("got PEMS")
} catch (error) {
console.log(error)
console.log('Error! Unable to download JWKs');
}
}
Where ive defined this globally let pems: { [key: string]: string } = {}, not sure exactly where my issue lies any help would be appreciated
Followed the exact tutorial to this however still stuck on this one error message, possibly the tutorial was outdated im not exactly sure
2
Answers
fetch
returns aPromise
that resolves to aResponse
.Response.json
returns a promise which resolves toJSON
, which does not contain akeys
property (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON).This is a destructuring assignment, which tries to unpack the
keys
property fromdata
.data
at that point is aJSON
object and doesn’t contain akeys
property.I’m not able to recreate exactly because the return type of
response.json
should beany
.What if you cast the result of
response.json
to your expected type? E.g.,