node.js - 如何修复节点文件系统写入功能?
问题描述
函数 writeFile 已识别,但在 ubuntu 18.04 中不起作用;但是,它确实适用于我的本地环境。
但它不写。
我为该文件夹添加了完全权限。
const fs = require('fs');
fs.writeFile( 'test.json', 'dfsdfsd', function(err){
if (err) {
return console.log(err);
}
console.log('data appended');
console.log(fs.writeFile);
});
结果
root@laravel-dropin:/var/www/html/resources# node js/test.js
data appended
[Function: writeFile]
解决方案
答案:这可以代替 writeFile 函数
let writeStream = fs.createWriteStream('secret.json');
// write some data with a base64 encoding
writeStream.write(s);
// the finish event is emitted when all data has been flushed from the stream
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
// close the stream
writeStream.end();
推荐阅读
- angular - 如何在承诺链中共享 Angular 服务?
- c# - 在 mongodb 中查找使用 System.Decimal 类型保存的值的总和/平均值
- c# - 使用索引/范围对数组进行切片是否会创建数组的副本
- android - AcknowledgePurchase 如何真正在 Android 上运行?
- yii2 - Yii2 detailView 属性提示
- javascript - chrome扩展中的CORS
- scala - scala 实现多种泛型类型
- python - 带有 Keras 的 Mask-RCNN:尝试将“形状”转换为张量但失败了。错误:不支持无值
- botframework - 如何使用自适应卡添加 RTF 输入框?
- amazon-web-services - 以编程方式创建 AWS Athena 视图