I'm trying to use the Flickr API to display some of my public photos on a website. However I'm getting some errors that I absolutely can't figure out. The weirdest thing is that the code worked fine a couple of days ago, and debugging reveals that the query goes through as intended. The problem seems to lay in resolving the promise that fetch() returns.
function getPhotos(){
const url = `${URL}${queryString('getPhotos')}`;
fetch(url)
.then((resp) => {
const w = resp.json();
return w;
// get different results if with
// return resp.json();
});
}
Here in debugging mote, using the uncommented method, the status of the promise ends up as fulfilled. Looking at the json object I can see all of the photos there under . If I do return resp.json() directly, it returns undefined
async function generatePhotos(list) {
const list = await getPhotos();
list.then((resp) => {
// always get 'list is undefined' error
// JSON.parse(list);
const photos = resp.photo;
for (let i = 0; i < 3; i++) {
generateImgElem(photos[i].id);
}
});
}
This function wont work, no matter what is returned from getPhotos(), the promise always comes out as pending.
What am I doing wrong here? I've searched around a bunch and as far as I can tell I'm using the fetch api correctly.
Edit: I'm getting errors that the variable list is undefined.
function getPhotos(){
const url = `${URL}${queryString('getPhotos')}`;
fetch(url)
.then((resp) => {
return resp.json();
});
// passes in undefined whether I stringify or not
// .then((json) => {
// return JSON.stringify(json);
// });
}