JS - ReferenceError: fetch is not defined
You're running it on Node JS instance in Paiza.io. Use CodeSandbox or CodePen for running your code please. And here... Paiza runs on Node JS not on Browser.
In your example, you need to use fetch()
this way:
fetch(
"https://sochain.com/api/v2/address/LTC/LMSuo8W7CiXs8oFs1sJh77AQ54tCZM42Ay"
)
.then((res) => res.json())
.then((obj) => document.write(obj["data"]["received_value"]));
ReferenceError: fetch is not defined - Postman
Does Postman not implement the Fetch API?
I think not. The closest correspondence to the fetch()
command
is pm.sendRequest()
.
But pm.sendRequest returns a pm object and not a Promise,
at least for now.
I have found a workaround, though.
In the code snippet below I define the pmFetch()
function which is meant to
do what the fetch()
command does in a normal web browser.
pmFetch('https://jsonplaceholder.typicode.com/todos/3')
.then(response => response.json())
.then(responseBody => {
console.log('The response body:');
console.log(responseBody);
console.log('responseBody.title: "' + responseBody.title + '"');
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function pmFetch (url) {
return new Promise ((resolve, reject) => {
pm.sendRequest(url, function (err, response) {
if (err) reject(err);
resolve(response);
});
});
}
Here is a link to the Postman Collection
in case you want to download, import, and run the collection.
Reference:
- How to make pm.sendRequest return a Promise
D3.CSV ReferenceError: fetch is not defined
the following should work for you.
source
You need an environment that supports the Fetch API, such as a a web browser. Node does not currently support Fetch, though it may in the future. If you want to load this library in an environment that does not natively support Fetch you will need to load your own polyfill such as node-fetch.
Related Topics
JavaScript Use Variable as Object Name
What Does $.When.Apply($, Somearray) Do
Setting Multiple Attributes for an Element at Once with JavaScript
How to Remove Spaces from a String Using JavaScript
How to Animate Scrolltop with Jquery
Differencebetween Parseint(String) and Number(String) in JavaScript
JavaScript - Return String Between Square Brackets
Angularjs Multiple Filter with Custom Filter Function
Why Is Immutability So Important (Or Needed) in JavaScript
Componentdidmount Equivalent on a React Function/Hooks Component
How to Serialize an Object into a List of Url Query Parameters
How to Remove Only the Parent Element and Not Its Child Elements in JavaScript
How to Access a Child's State in React
App.Settings - the Angular Way
Is It Safe to Assume Strict Comparison in a JavaScript Switch Statement