Posts

Showing posts with the label fetch

javascript fetch api template string

javascript fetch api template string I'm trying to make an app that gives you the weather based on your location. I get the location with ipinfo, and it works. I put the location in a variable, and when I console log it it still works. But when I try to fetch data from openweathermap with that variable using a template string this is what the console shows: Uncaught (in promise) ReferenceError: currentCity is not defined at getWeather The problem is when I copy that link to the console it returns a link that works! Thanks for the help const getLocation = async () => { const response = await fetch('http://ipinfo.io/json?token=(id given by ipinfo)') if (response.status === 200) { const data = await response.json() return data.city } else { throw new Error('Unable to get the current location') } } let currentCity getLocation().then((city) => { currentCity = city ...

GET with query string with Fetch in React Native

GET with query string with Fetch in React Native I am making a request like this: fetch("https://api.parse.com/1/users", { method: "GET", headers: headers, body: body }) How do I pass query string parameters? Do I simply add them to the URL? I couldn't find an example in the docs. Possible duplicate of Setting query string using Fetch GET request – Hirurg103 Jun 30 at 14:29 4 Answers 4 Your first thought was right: just add them to the URL. Remember you can use template strings (backticks) to simplify putting variables into the query. const data = {foo:1, bar:2}; fetch(`https://api.parse.com/1/users?foo=${encodeURIComponent(data.foo)}&bar=${encodeURIComponent(data.bar)}`, { method: "GET", headers: headers...