微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AngularJS,NodeJS:将数据保存到Json文件或数据库中

如何解决AngularJS,NodeJS:将数据保存到Json文件或数据库中

我是AngularJS和nodeJS的新手,但我似乎找不到我需要的信息,我希望这里有人能提供帮助。 我正在创建这个小应用程序作为培训,它基本上是一个个人词典:用户(目前只有我一个人)可以输入他们想要保留的所有信息,并在需要时进行引用。

我的问题是存储部分,即数据库。 我去了一个简单的json文件,我可以很容易地通过AngularJS应用程序阅读它。但是,当我需要存储新条目时会遇到麻烦。 经过一番阅读后,我意识到POST请求无法正常工作,因为javascript,angularjs或nodejs不允许写入本地文件(或一般而言),并且我需要通过另一项服务才能使之成为可能。 。正确吗?

我最好共享我的不同文件

1 /我的index.html调用form指令:

    <div class="container" ng-controller="EntryController as ctrl">
        <entry-form data="ctrl.entry" submit="ctrl.submitEntry();"></entry-form>
    </div>

2 /我的表单指令:

function entryForm () {
    return {
        restrict: 'E',scope: {},bindToController: {
            data: '=',submit: '&',},controller: 'FormController as form',templateUrl: 'templates/entry-form.html'
    };
};


angular
    .module('app')
    .directive('entryForm',entryForm);

3 /我的表单模板:

<form name="orderForm" ng-submit="form.onSubmit()">
    <input required="" ng-minlength=2 ng-maxlength=15 name="title" type="text" ng-model="form.data.title" placeholder="Title">
    <input required="" name="topic" type="text" ng-model="form.data.topics" placeholder="Topic1">
    <input name="topic" type="text" ng-model="form.data.topics" placeholder="Topic2">
    <input name="topic" type="text" ng-model="form.data.topics" placeholder="Topic3">
    <textarea required="" name="text" ng-model="form.data.content" placeholder="Content" name="" id="" cols=42 rows="5"></textarea>
    <input name="link" type="text" ng-model="form.data.link" placeholder="Link">
    <input required="" name="category" type="text" ng-model="form.data.mainCategory" placeholder="Main category">
    <button type="submit">Submit</button>
</form>

4 /我的表单控制器:

function FormController () {
    this.onSubmit = function () {
        console.log('submitting at formController level')
        this.submit();
    };
};

angular
    .module('app')
    .controller('FormController',FormController);

5 /我的模型控制器:

function EntryController ($http) {
    const ctrl = this;
    const API = '../database/lexicon.json';

    this.entry = {
    title: "",topics: [],content: "",link: "",mainCategory: ""
    };
    this.submitEntry = function () {
        // communicate with the API
        console.log("testing");
        console.log(this.entry);
        $http
        .post(API,"hello my friend")
        .then(function() {
            console.log("successful");
        });
    };
};


angular
    .module('app')
    .controller('EntryController',EntryController);

6 /我的词典控制器:

function LexiconController ($http) {
    const ctrl = this;
    const API = '../database/lexicon.json';
    this.lexicon = [];
    $http
        .get(API)
        .then(function (response) {
            ctrl.lexicon = response.data.entries;
        });
};

angular
    .module('app')
    .controller('LexiconController',LexiconController)

我希望我什么都不会错过,否则请告诉我。 当然,我的POST方法无效,并返回405 (Method Not Allowed)

您如何建议我进行处理,以便用户可以对该json文件执行所有必要的CRUD操作? (顺便说一下,用我手动输入的数据)

{
    "entries": [{
        "title": "testing","topics": [],"content": "","link": "","mainCategory": "Rails"
        },{
        "title": "Trying again","mainCategory": "AngularJS"
    }]
}

谢谢!

解决方法

我会看看lowdb,这正是它的设计目的。

例如,假设文件“ entries.json”:

entries.json

{
    "entries": [{
        "title": "testing","topics": [],"content": "","link": "","mainCategory": "Rails"
        },{
        "title": "Trying again","mainCategory": "AngularJS"
    }]
}

test.js

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const dbFileName = "entries.json";
const adapter = new FileSync(dbFileName)
const db = low(adapter)

// Add another entry
db.get('entries')
.push({
    "title": "Here's a new entry","mainCategory": "Node.js"
    })
.write();

  

这最终看起来像这样:

{
"entries": [
    {
      "title": "testing","mainCategory": "Rails"
    },{
      "title": "Trying again","mainCategory": "AngularJS"
    },{
      "title": "Here's a new entry","mainCategory": "Node.js"
    }
]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?