async function getMovieList(year) { const response= await…
- User
async function getMovieList(year) {
const response= await fetch(https://jsonmock.hackerrank.com/api/movies?Year=${year}); const data = await response.json(); const movieList= data.data.map(movie=>movie.Title); return movieList } async function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH);const year = readLine().trim();
const results = await getMovieList(year);
if (results.length > 0) { for (const result of results) { ws.write(
${result}\n); } } else { ws.write('No Results Found'); }ws.end(); } why above code give this error (node:207773) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time (Use
node --trace-warnings ...to show where the warning was created) node:internal/deps/undici/undici:4045 if (socket[kParser].timeoutType !== TIMEOUT_IDLE) { ^ TypeError: Cannot read properties of undefined (reading 'timeoutType') at _resume (node:internal/deps/undici/undici:4045:33) at resume (node:internal/deps/undici/undici:4015:7) at connect (node:internal/deps/undici/undici:4004:7) Node.js v18.6.0 - Assistant
The error is not related to the code itself, but rather a bug in Node.js v18.6.0 with the
undicilibrary that is used by thefetchAPI. The error seems to occur when the request takes too long to complete and the socket times out. This bug has been reported and is being fixed in later versions of Node.js. In the meantime, you could try downgrading to an earlier version of Node.js or using a different library for making HTTP requests. - User
changee library
- Assistant
Sure! Instead of using the
fetchAPI, you can use theaxioslibrary to make HTTP requests:First, you'll need to install
axiosby running the commandnpm install axios.Then, you can modify the
getMovieListfunction to useaxiosinstead offetch:const axios = require('axios'); async function getMovieList(year) { const { data } = await axios.get(`https://jsonmock.hackerrank.com/api/movies?Year=${year}`); const movieList= data.data.map(movie=>movie.Title); return movieList }In this implementation, we're using
axios.getinstead offetch, and we're destructuring the response to get thedatadirectly.Now, in the
mainfunction, you don't need to modify anything else, as the rest of the code is already working with the returned array of strings.I hope this helps!
- User
/tmp/submission/20230526/11/45/hackerrank-7a72eaf883111e8c53f953684b57abdc/code/Solution.js:28 const data = await response.json(); ^ TypeError: response.json is not a function at getMovieList (/tmp/submission/20230526/11/45/hackerrank-7a72eaf883111e8c53f953684b57abdc/code/Solution.js:28:33) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async main (/tmp/submission/20230526/11/45/hackerrank-7a72eaf883111e8c53f953684b57abdc/code/Solution.js:38:19) Node.js v18.6.0
- Assistant
The error you're getting is because the
responseobject returned byaxiosdoes not have ajson()method. Instead, Axios returns the response data as a JavaScript object.You can modify the
getMovieListfunction to handle the Axios response data like this:async function getMovieList(year) { const response = await axios.get(`https://jsonmock.hackerrank.com/api/movies?Year=${year}`); const movieList = response.data.data.map(movie => movie.Title); return movieList; }Here, we're using destructuring to get the
dataobject directly from theresponse. Then we're usingmap()to create an array of movie titles.This should fix the error you're seeing.
- User
/tmp/submission/20230526/11/46/hackerrank-ec1537c25f99b02a70671dde941d4b9b/code/Solution.js:29 const data = await response.json(); ^ TypeError: response.json is not a function at getMovieList (/tmp/submission/20230526/11/46/hackerrank-ec1537c25f99b02a70671dde941d4b9b/code/Solution.js:29:33) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async main (/tmp/submission/20230526/11/46/hackerrank-ec1537c25f99b02a70671dde941d4b9b/code/Solution.js:38:19) Node.js v18.6.0
- Assistant
The error message you're seeing is similar to the previous error message. It looks like your
getMovieListfunction is still trying to call the non-existentjson()method on the Axios response object.To fix this error, you can modify the
getMovieListfunction to handle the Axios response data like this:async function getMovieList(year) { const response = await axios.get(`https://jsonmock.hackerrank.com/api/movies?Year=${year}`); const movieList = response.data.data.map(movie => movie.Title); return movieList; }Here, we're using destructuring to get the
dataobject directly from theresponse. Then we're usingmap()to create an array of movie titles.This should fix the error you're seeing.
- User
/tmp/submission/20230526/11/48/hackerrank-3b8778cd649e578246f5b174bccbfe2a/code/Solution.js:30 const movieList= response.data.map(movie=>movie.Title); ^ TypeError: response.data.map is not a function at getMovieList (/tmp/submission/20230526/11/48/hackerrank-3b8778cd649e578246f5b174bccbfe2a/code/Solution.js:30:36) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async main (/tmp/submission/20230526/11/48/hackerrank-3b8778cd649e578246f5b174bccbfe2a/code/Solution.js:38:19) Node.js v18.6.0
- Assistant
The error message you're seeing is indicating that
response.datais not an array, and therefore themap()method is not available to use.To fix this error, you need to make sure that you're accessing the correct JSON data in the response object. It seems like the
dataarray is nested under adataproperty in the response object. You can modify thegetMovieListfunction like this to extract the correct data:async function getMovieList(year) { const response = await axios.get(`https://jsonmock.hackerrank.com/api/movies?Year=${year}`); const movieList = response.data.data.map(movie => movie.Title); return movieList; }Here, we're using destructuring to get the
dataobject directly from theresponse. Then we're accessing thedataarray in thedataobject and usingmap()to create an array of movie titles.This should fix the error you're seeing.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.