[英]Node - Write JSON result to a file after a promise
I am using request-promise to fetch data from an api. 我正在使用请求承诺从api获取数据。 I need to write the results to a json file.
我需要将结果写入json文件。 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. 我已经试过这也,但没有任何解决。
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)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.