Problem with Fetch (TypeError: Network request failed: Can't find variable: FormData)

fetch

#1

I can’t get Fetch to work. I’m writing a function to pull data from an API. I kept getting the error: “TypeError: Network request failed: Can’t find variable: FormData”

After messing with it for a while and digging through code and googling to no avail, I literally copy/pasted the code from the NS docs, and I get the same error.

Here’s the code I’m using. Aside from the module declaration, it’s copied straight from the NS docs:

const fetchModule = require("fetch");

fetchModule.fetch("https://httpbin.org/get")
.then((response) => response.text())
.then((r) => {
    console.log("HERE: " + r);
}).catch((e) => {
	console.error(e);
});

I find it hard to believe no one else has had this problem, but I can’t find any discussion about it here or anywhere else.

Any help?


#2

That sounds strange, are you using FormData anywhere in your application?
May I know which version of NativeScript CLI & Runtime you are using? Which platform you are testing it with?


#3

Nope. But, I just figured out the solution … or, at least, something that works.

If I add options to the fetch call it works fine:

fetchModule.fetch(url, { method: "GET" }).then(...)

It would be nice if someone updated the NS docs to reflect this. I find it odd, though, that no one else seems to have had this problem.