AngularJS + Electron:将数据保存到 JSON 文件

AngularJS + Electron: Save Data to JSON File

我是 AngularJS 和 Electron 的新手,我目前正在开发一个简单的桌面应用程序,它从 JSON 文件中读取数据并允许用户更新和删除数据.我还使用 TaffyDB 来查询数据。我可以从 JSON 文件中获取数据,但无法将其存储在 JSON 文件中。

到目前为止我尝试的是:

myApp.controller('homeController', ['$scope', '$http', function($scope, $http) {

    $scope.saveData = function() 
    {
        var data = $scope.data;

        $http.post('/src/db/db.json', data).then(function (response) {

            console.log(response);

        }, function (response) {

            console.log(response);

        });
    };

}]);

在浏览器上执行事件时出现以下错误:

POST http://127.0.0.1:64262/src/db/db.json 404 (Not Found)

这是正常的,因为我们无法从 javascript 访问用户的文件系统。

当我将应用程序作为 Electron 包执行时,我得到以下信息:

Object {data: Array[2], status: 200, config: Object, statusText: "OK"}

但是文件没有被修改。

我想知道是否有任何方法可以使用 AngularJS 和 Electron 来完成。

重要提示:此应用程序需要运行作为独立的桌面应用程序。换句话说,我的客户端不需要安装什么其他软件或应用程序就可以使用这个应用程序。

您不能使用 HTTP 写入文件系统,这仅适用于网络。

Electron 基于 Node.js 构建。所以看看Node.js File System module。例如:

fs.writeFile('/src/db/db.json', data, (err) => {
  if (err) throw err;
  console.log('It\'s saved!');
});