I'm tring to use a coin API website for a project, I keep encountering the same error:
var myHeaders = new Headers();
^
ReferenceError: Headers is not defined
at Object.<anonymous> (C:\Users\magshimim\say-hi-to-sheli-from-me\rafaels-crypto-ninja\thingy\server\index.js:14:17)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
that's the code in use:
var myHeaders = new Headers();
myHeaders.append("apikey", "the API key is here");
var requestOptions = {
method: 'GET',
redirect: 'follow',
headers: myHeaders
};
const data = fetch("https://api.apilayer.com/fixer/timeseries?start_date=2021-01-01&end_date=2021-12-31", requestOptions)
.then(response => response.text())
//.then(result => console.log(result))
.then(result => result)
.catch(error => console.log('error', error));
console.log(requestOptions);
CodePudding user response:
Please check this as you should fetch the hearers first.
const fetch = require("node-fetch");
var myHeaders = new fetch.Headers();