I'm trying to make a JavaScript fetch request which I wrote custom and then I took the Postman version which works in Postman but not in vscode.
The errors I am seeing using the console in VSC are: Uncaught ReferenceError ReferenceError: Headers is not defined and No debugger available, can not send 'variables'
So, this works in Postman so I know the request works but I'll add the code anyway. I've also tried to add in my package.json file: "console": "integratedTerminal", and I am importing import fetch from "node-fetch";
The full code looks like this:
import fetch from "node-fetch";
// added
import { Headers} from "node-fetch";
import { Buffer } from 'buffer';
const SAUCE_USERNAME = process.env.SAUCE_USERNAME;
const SAUCE_ACCESS_KEY = process.env.SAUCE_ACCESS_KEY;
let euUrl = `https://api.eu-central-1.saucelabs.com/rest/v1/${SAUCE_USERNAME}/jobs`;
let usUrl = `https://api.us-west-1.saucelabs.com/rest/v1/${SAUCE_USERNAME}/jobs`;
// What information do you want to fetch?
// gets all jobs
// /rest/v1/{username}/jobs <- returns an empty object, you need to specify a time range
// this gets the last week of jobs, you need to use a unix time stamp
// see https://www.unixtimestamp.com/ for more info
let timeRange = '?from=604800';
// gets specific job
// /rest/v1/{username}/jobs/{job_id}
let myHeaders = new Headers();
myHeaders.set("Authorization", "Basic " + Buffer.from(SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY).toString('base64'));
console.log(myHeaders)
let requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch(euUrl + timeRange, requestOptions)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log('error', error));
Not sure what I could be missing and spent a while looking at this and other stack overflows which suggest breakpoints and so on as I also see: No debugger available, can not send 'variables' Process exited with code 1
I've added breakpoints using the red dots next to the lines of code and just stuck now. Would appreciate any help.
Another edit: Added additional breakpoint which gives me a breakdown after the fetch is made, but it doesn't show me any data... any reason why?
1条答案
按热度按时间wgmfuz8q1#
我通过添加以下内容解决了这个问题:从“buffer”导入{ Buffer };
请参阅上面编辑过的代码以获取最终答案。