I'm a learner on currently following a YouTube tutorial on JS, And the below code worked fine in the video, but I get an error message
error: Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Console log shows the line which has the code console.log("finished!");
as the line with the error
The JS code:
const getDataFromForm = () => {
const requestObj = {
firstName: "Humaid",
lastName: "Ahamed",
categories: ["nerdy"],
};
return requestObj;
};
const buildRequestUrl = (requestData) => {
`http://api.icndb.com/jokes/random?firstName=${requestData.firstName}&lastName=${requestData.lastName}&limitTo=${requestData.categories}`;
};
const requestJokeAnotherMethod = async (url) => {
const Response = await fetch(url);
const jsonResponse = await Response.json();
const joke = jsonResponse.value.joke;
postJokeToPage(joke);
};
const postJokeToPage = (joke) => {
console.log(joke);
};
const processJokeRequest = async () => {
const requestData = getDataFromForm();
const requestUrl = buildRequestUrl(requestData);
await requestJokeAnotherMethod(requestUrl);
console.log("finished!");
};
processJokeRequest();
CodePudding user response:
Code is missing at the beginning. Perhaps cut off when inserting here in stackoverflow?
CodePudding user response:
I've notice that I've forgot to return the Chuck Norris API link, Now the code works like charm, Thank you for that person ho pointed out my mistake
I had previously not included the return
statement in the below function
return `http://api.icndb.com/jokes/random?firstName=${requestData.firstName}&lastName=${requestData.lastName}&limitTo=${requestData.categories}`;
};```