简体   繁体   中英

node.js how to use fs for received get api result

I'm a newbie at node.js. here I'm trying to create a txt file with my received result(response.body). but it just prints an exception. need your kind help.

function fetch(){

const got = require('got');

got('https://www.google.com', { json: true }).then(response => {

    console.log(response.body);
    temp = response.body;

}).catch(error => {
    console.log(error.response.body);
});
};


setInterval(fetch, 10000);

const fs = require('fs');
fs.write('demo3.txt', response.body, { flag: "a"}, function(err){

if(err){
    return console.log(err);
}
});

Three problems:

1 - temp and response.body are both scoped within your fetch() function, and therefore cannot be used outside.

2 - your handling of async operations is incorrect. While setInterval will delay the fetch by 10 seconds, the writing to the text file will still run immediately.

3 - you are using the async file writing method at the end of the file. IIRC the program will terminate before this can complete.

A naive solution would be to make a variable outside fetch for storing the value, running fetch, and then having the writing operation delayed by 10 seconds.

However it is not ideal to wait an arbitrary amount of time for an async operation to finish before proceeding.

If you're willing to use ES2017/ES8 I propose the following code:

const got = require('got');
const fs = require('fs');

async function fetch() {
  const response = await got('https://www.google.com');
  return response.body;
};

fetch().then(body => fs.writeFileSync('./demo3.txt', body));

(PS! I removed the option {json:true} from got as google.com seems to only return html as far as I could tell. Not sure if this was important to whatever it is you were trying to do.)

In case you're unfamiliar with the syntax:

async - declares an async operation. It is straightforward to use as either a Promise (as done at the end of the file) or Observable.

await - tells the program to halt execution of code until the async operation on the same line has resolved. In this case, operation waits for a response from google.com

writeFileSync - Changed this from write to make sure the processor waits for the process to finish before terminating. This might or might not be necessary depending on under which circumstances you are running this program.

as you are new to nodeJS, you need to know to concepts of callback functions. Here 'got' is a callback function. Unlike other types of scripts, NodeJs won't wait for fetch to get completed, rather it will move forward and execute the remaining statements. Your code has got a few mistakes.

  1. Scoping of variables 'temp', 'response'. -- At the time of execution of fs.write, the response.body remains undefined and undeclared. As fetch function didn't get executed yet.

  2. You sent a parameter { json: true }, this parameter tries to get the response in JSON format. But the requested URL replies with a normal text document / HTML document. So removing that parameter will surely help.

     const fs = require('fs'); const got = require('got'); function fetch(){ got('https://www.google.com' ).then(response => { console.log("Response"); temp = response.body; fs.writeFile('demo3.txt', temp , { flag: "a"}, function(err){ if(err){ return console.log(err); } }); }).catch(error => { console.log(error); }); }; fetch(); 

Here fs.write will execute after the 'got' function completes its execution by returning response variable. Here I have also removed the parameter { json: true } , as the response is not in JSON format.

function fetch(){
   var fs = require('fs');
   var got = require('got');

    got('https://www.google.com', { json: true }).then(response => {

    console.log(response.body);
    temp = response.body;
     fs.writeFile("/tmp/test",temp, function(err) {
        if(err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });

    }).catch(error => {
       console.log(error.response.body);
    });
};    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM