简体   繁体   English

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

[英]AngularJS + Electron: Save Data to JSON File

I'm very new to AngularJS and Electron, and i'm currently working on a simple desktop app that reads data from a JSON file and allows the user also update and delete data. 我是AngularJS和Electron的新手,目前正在开发一个简单的桌面应用程序,该应用程序可从JSON文件读取数据,并允许用户更新和删除数据。 I'm also using TaffyDB to query the data. 我也在使用TaffyDB查询数据。 I'm able get data from the JSON file but unable to store it in the JSON file. 我可以从JSON文件中获取数据,但无法将其存储在JSON文件中。

What i tried so far was this: 到目前为止,我尝试过的是:

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);

        });
    };

}]);

Executing the event on the browser i get the following error: 在浏览器上执行事件我得到以下错误:

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

This is normal because we cannot access the user's filesystem from javascript. 这是正常现象,因为我们无法从javascript访问用户的文件系统。

When i execute the app as a Electron package, i get the following: 当我以电子程序包的形式执行该应用程序时,我得到以下信息:

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

But the file was not modified. 但是该文件未修改。

I would like to know if there is any way i can accomplish this using AngularJS and Electron. 我想知道是否有什么方法可以使用AngularJS和Electron完成此任务。

Important Note : This app needs to run as a standalone desktop application. 重要说明 :此应用程序需要作为独立的桌面应用程序运行。 In other words, my client doesn't what to install other software or apps to be able to use this application. 换句话说,我的客户不需要安装其他软件或应用程序就能使用该应用程序。

You can't write to the file system using HTTP, that's for the web only. 您不能使用HTTP写入文件系统,仅用于Web。

Electron is built on Node.js. Electron基于Node.js构建。 So take a look at the Node.js File System module . 因此,请看一下Node.js文件系统模块 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM