AD B
AD B

Reputation: 204

How to create and update a text file using React.js?

I am trying to save a variable's data into a text file and update the file every time the variable changes. I found solutions in Node.js and vanilla JavaScript but I cannot find a particular solution in React.js.
Actually I am trying to store Facebook Long Live Access Token in to a text file and would like to use it in the future and when I try importing 'fs' and implementing createFile and appendFile methods I get an error saying Method doesn't exist.
Please help me out. Here is the code below

window.FB.getLoginStatus((resp) => {

        if (resp.status === 'connected') {
            const accessToken = resp.authResponse.accessToken;
            try {
                axios.get(`https://graph.facebook.com/oauth/access_token?client_id=CLIENT_id&client_secret=CLIENT_SECRET&grant_type=fb_exchange_token&fb_exchange_token=${accessToken}`)
                    .then((response) => {
                        console.log("Long Live Access Token " + response.data.access_token + " expires in " + response.data.expires_in);
                        let longLiveAccessToken = response.data.access_token;
                        let expiresIn = response.data.expires_in;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            }
            catch (e) {
                console.log(e.description);
            }
        }

    });

Upvotes: 2

Views: 8149

Answers (1)

Miguel Calderón
Miguel Calderón

Reputation: 3091

React is a frontend library. It's supposed to be executed in the browser, which for security reasons does not have access to the file system. You can make React render in the server, but the example code you're showing is clearly frontend code, it uses the window object. It doesn't even include anything React-related at first sight: it mainly consists of an Ajax call to Facebook made via Axios library.

So your remaining options are basically these:

  1. Create a text file and let the user download it.
  2. Save the file content in local storage for later access from the same browser.
  3. Save the contents in online storage (which could also be localhost).

Can you precise if any of these methods would fit your needs, so I can explain it further with sample code if needed?

Upvotes: 4

Related Questions