0

I'm trying to create multiple JSON objects using an external call out to FourSquare API with different query parameters, but my code keeps returning as a successful response code with no readable results. Here is the code I'm trying to use:

async function getPlaces(query, lat, lon, clientID, clientSecret, versionDate) {
    const URL = `https://api.foursquare.com/v2/venues/search?client_id=${clientID}&v=${versionDate}&ll=${lat},${lon}&intent=browse&radius=10000&query=${query}&limit=10&client_secret=${clientSecret}`;
    const response = await fetch(URL).catch(e => { console.log(e) });
    const data = await response.json().catch(e => { console.log(e) });

    return data;
}

When I call this function to create a JSON object like so

const beachData = getPlaces("beaches", lat, lon, clientID, clientSecret, versionDate);

It then returns as this:

Promise { <pending> }

I tried this with the same result

const beachData = getPlaces("beaches", lat, lon, fourSquareClientID, fourSquareClientSecret, versionDate).then(({ beachData }) => { beachData; });

Any guidance on what i'm doing wrong would be HUGELY appreciated. If I'm not totally off the mark I'd like to be able to call on this function a few times and get some JSON objects back I can later combine for the response back to the front end. Thanks!

2
  • I don't understand how you can know that you need to await the promise returning function fetch() yet not know that you need to await the promise returning function getPlaces() Commented Jan 13, 2021 at 5:28
  • Upon review that was an oversight, haha.Was working late and definitely overlooked that. Thanks for pointing that out, haha. Commented Jan 13, 2021 at 19:52

1 Answer 1

1

If you try to log the return value of fetch;

let x = fetch(URL);
console.log(x);

You would get:

Promise { <pending> }

So the solution is of course to await it:

let x = await fetch(URL);

Similarly, you need to await your function:

const beachData = await getPlaces("beaches", lat, lon, clientID, clientSecret, versionDate);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.