I want to add strings to my JSON file which looks like this:
{
"items": []
}
which is required like this: var items = require("../../config/item.json");
Then I am writing it to the array like this: items["item"].push(str);,
which triggers an error: Cannot read property 'push' of undefined
How can I add the string "str" to the array?
After pushing it, i write it to the file like this:
let data = JSON.stringify(items);
fs.writeFileSync("../../config/item.json", data, (err) => {
if (err) {
throw err;
} else {
console.log("item written successfully.");
}
});
Thanks in advance.
items["item"].push(str);toitems["items"].push(str);