I am attempting to use the .map() function over an array in Typescript.
I run the following code:
Promisefunction().then((data : [{item: String}]) => {
data.map((Object) => {
console.log(Object.attribute)
})
})
In the console, I get the expected result and it seems to work fine but then the program crashes and I get the following error:
Unhandled Rejection (TypeError): Cannot read property 'map' of undefined
I assume this is because Typescript doesn't know Data is an array, yet I have established that up above. So I am left confused.
Any help would be appreciated, thank you.
**Edit 1: Added body of promisefunction **
export default function promiseFunction(){
return new Promise<any[]>(function(resolve, reject){
const { data, loading, error, fetchMore } = useQuery(
QUERY,
{
variables: {
first: 15,
offset: 0
},
errorPolicy: 'all'
});
if(data){
resolve(data.getData);
}
else if(error){
reject("broke");
}
})
}
Promisefunctiondoesn't return anything which is why data is undefined.datais undefined.