我从API实现了一个API,结果数据需要将其存储在.txt
文件中。当API调用时,数据应在.txt
文件写入。
此API调用包含从客户端接收的数据数组。将所得物添加元素的所有阵列成一个单一的阵列。要写入的Node.js文件中的单个阵列的需求。
我的代码是:
exports.Entry = functions.https.onRequest((req, res) => {
var fs = require('fs');
var a = ['6', '7', '8'];
var b = ['22', '27', '20'];
var eachrecord = [];
for (var i = 0; i < a.length; i++) {
eachrecord += a + b;
}
console.log("eachrecord is", eachrecord);
//Writing each record value into file
fileWriteSync('/Users/lahariareti/Desktop/deyaPaydevelopment/deyaPayandroidRETROFIT/functions/filewriting.txt');
function fileWriteSync(filePath) {
var fd = fs.openSync(filePath, 'w');
var length = eachrecord.length;
for (i = 0; i < length; i++) {
var eachrecordwrite = fs.writeSync(fd, eachrecord[i] + '\n', null, null);
console.log("hii", eachrecord[i]);
}
fs.closeSync(fd);
}
});
当我部署和测试它显示了错误等:
“错误:ENOENT:没有这样的文件或目录时,开 '/Users/lahariareti/Desktop/deyaPaydevelopment/deyaPayandroidRETROFIT/functions/filewriting.txt'“
如何使用Node.js通过API调用将数据写入文件?
答案 0 :(得分:0)