Reputation: 199
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.
Upvotes: 0
Views: 2004
Reputation: 9
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)
});
Upvotes: 1