问题描述
我对Angular.js和node.js还是很陌生,自今天早上以来,我面临的一个问题已得到部分解决。如何使用用户通过表单输入的数据更新json文件。
由于我在这里得到了一些帮助,所以我设法使用了lowdb
来完成此任务,它看起来像是一个很棒的依赖项。事实是,我只能从测试文件中使它工作,就像这样:
1 / test.js(我运行node test.js
)
const FileSync = require('lowdb/adapters/FileSync')
const dbFileName = "database/lexicon.json";
const adapter = new FileSync(dbFileName)
const db = low(adapter)
// Add another entry
db.get('entries')
.push({
"title": "Here's a new entry","topics": ["ajax","rails"],"content": "This is very complicated","link": "","mainCategory": "Node.js"
})
.write();
2 /我想在哪里工作的控制器:
function EntryController ($http) {
const ctrl = this;
const API = '../database/lexicon.json';
this.entry = {
title: "",topics: [],content: "",link: "",mainCategory: ""
};
this.submitEntry = function () {
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const dbFileName = "database/lexicon.json";
const adapter = new FileSync(dbFileName)
const db = low(adapter)
// Add another entry
db.get('entries')
.push({
"title": "Number 1","mainCategory": "Node.js"
})
.write();
};
};
angular
.module('app')
.controller('EntryController',EntryController);
但是在运行应用程序并提交表单时,出现此错误消息require is not defined
我尝试了与import
类似的操作,但收到类似的错误消息:Cannot use import statement outside a module
有人可以帮忙吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)