I am very new to Angular.js and node.js and I am facing a problem since this morning that has been partially solved. How to update a json file with data entered by a user via a form.
Thanks to some help that I got here, I managed to do that using lowdb
which looks like an awesome dependency. The thing is, I can only make it work from a test file, like so:
1/ test.js (that I run 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/ Controller from where I would like it to work:
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",
"topics": ["ajax", "rails"],
"content": "This is very complicated",
"link": "",
"mainCategory": "Node.js"
})
.write();
};
};
angular
.module('app')
.controller('EntryController', EntryController);
But when running the app and submitting the form I get this error message require is not defined
I tried something similar with import
but I get a similar error message: Cannot use import statement outside a module
Can anybody help?
Go to Source
Author: Olivier Girardot