0

I am using request-promise to fetch data from an api. I need to write the results to a json file. Following code doesn't write anything to the file.

var rp = require('request-promise');
rp(empOptions)
    .then(function (repos) {
        employees= repos;
        return new Promise(function(resolve, reject) {
            fs.writeFile('../employees.json', JSON.stringify(employees), function(err) {
                if (err) reject(err);
            });
        });
    })
    .catch(function (err) {
        // API call failed...
    });

I have tried this also, but that didn't worked out either.

Luke
  • 199
  • 2
  • 2
  • 13
  • Have you checked if repos contains data in your `.then` method? – Kyle Richardson Jul 22 '17 at 01:06
  • Yes, it contains data. – Luke Jul 22 '17 at 01:13
  • 1
    Try removing the return new Promise. You're not returning data with the resolve method of the Promise anyhow. The fs.writeFile should work by itself in this situation. – Kyle Richardson Jul 22 '17 at 01:15
  • Already tried that and it didn't work either. – Luke Jul 22 '17 at 01:23
  • Have you tried something like `var stringifiedJSON = JSON.stringify(employees);` and then pass the `stringifiedJSON` variable into the `fs.writeFile()` method? – Kyle Richardson Jul 22 '17 at 01:24
  • Yes, tried that also Kyle – Luke Jul 22 '17 at 01:27
  • Well then I'm sorry I can't help. There is nothing wrong with the format of the `fs.writeFile()` method. Try this example `fs.writeFile('message.txt', 'Hello Node.js', (err) => { if (err) throw err; console.log('The file has been saved!'); });` That is straight from the File System documentation. Replace 'message.text' with the location you want the file and see if it works. – Kyle Richardson Jul 22 '17 at 01:29
  • Using ../employees.json actually created the file out of the project folder. Actually the file was created earlier too. Thanks for clearing this up. Thank you for the help. – Luke Jul 22 '17 at 01:35
  • You're welcome, glad you got it figured out! – Kyle Richardson Jul 22 '17 at 01:42

1 Answers1

1

Best and easy way to write on file:

.then(function(results) {
    return new Promise(function(resolve, reject) {
        fs.appendFileSync('myurlss2.json', results, function(err) {
            if (err) reject(err)
            else resolve(results)
        })
    })
})
.then(function(results) {
    console.log("results here: " + results)
})
.catch(function(err) {
    console.log("error here: " + err)
});
Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
SafiUllah
  • 9
  • 1
  • Full Code : .then(function(results) { return new Promise(function(resolve, reject) { fs.appendFileSync('myurlss2.json',results, function(err) { if (err) reject(err); else {resolve(results); } }); }); }).then(function(results) { console.log("results here: " + results) }).catch(function(err) { console.log("error here: " + err); }); – SafiUllah Jun 03 '19 at 06:30
  • instead of adding code in a comment, it is better to edit the answer, – dkb Jun 03 '19 at 06:31