Skip to content
Advertisement

Fetch json data from url and write in a file

I’m trying to fetch Json data from a Url and then write the data in a Json File. Here is my code :

JavaScript

But I’m stuck with this error as the data I want to write in my file seems to have an invalid argument while I use JSON.stringify. Someone got an idea ?

Thanks a lot for your help !

TypeError [ERR_INVALID_ARG_TYPE]: The “data” argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

Advertisement

Answer

jsondata is a redundant variable. Here is a rewrite of your fetch().then().then() which leverages fs.writeFile() in the second .then().

I used node-fetch for this implementation, but it should work in a browser environment as well.

JavaScript
Advertisement