-1

I am making a discord bot in Node.js mostly for fun to get better at coding and i want the bot to push a string into an array and update the array file permanently.

I have been using separate .js files for my arrays such as this;

module.exports = [
  "Map: Battlefield",
  "Map: Final Destination",
  "Map: Pokemon Stadium II",
];

and then calling them in my main file. Now i tried using .push() and it will add the desired string but only that one time.

What is the best solution to have an array i can update & save the inputs? apparently JSON files are good for this.

Thanks, Carl

Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
  • you could use [node-json-db](https://www.npmjs.com/package/node-json-db) allows you to use a JSON file as a 'database' & exposes methods such as 'push' – Dan Starns Sep 19 '19 at 13:13

1 Answers1

0

congratulations on the idea of writing a bot in order to get some coding practice. I bet you will succeed with it!

I suggest you try to split your problem into small chunks, so it is going to be easier to reason about it.

Step1 - storing

I agree with you in using JSON files as data storage. For an app that is intended to be a "training gym" is more than enough and you have all the time in the world to start looking into databases like Postgres, MySQL or Mongo later on.

A JSON file to store a list of values may look like that:

{
  "values": [
    "Map: Battlefield",
    "Map: Final Destination",
    "Map: Pokemon Stadium II"
  ]
}

when you save this piece of code into list1.json you have your first data file.

Step2 - reading

Reading a JSON file in NodeJS is easy:

const list1 = require('./path-to/list1.json');
console.log(list.values);

This will load the entire content of the file in memory when your app starts. You can also look into more sophisticated ways to read files using the file system API.

Step3 - writing

Looks like you know your ways around in-memory array modifications using APIs like push() or maybe splice().

Once you have fixed the memory representation you need to persist the change into your file. You basically have to write it down in JSON format.

Option n.1: you can use the Node's file system API:

// https://stackoverflow.com/questions/2496710/writing-files-in-node-js
const fs = require('fs');

const filePath = './path-to/list1.json';
const fileContent = JSON.stringify(list1);

fs.writeFile(filePath, fileContent, function(err) {
  if(err) {
    return console.log(err);
  }

  console.log("The file was saved!");
}); 

Option n.2: you can use fs-extra which is an extension over the basic API:

const fs = require('fs-extra');

const filePath = './path-to/list1.json';

fs.writeJson(filePath, list1, function(err) {
  if(err) {
    return console.log(err);
  }

  console.log("The file was saved!");
});

In both cases list1 comes from the previous steps, and it is where you did modify the array in memory.

Be careful of asynchronous code:

Both the writing examples use non-blocking asynchronous API calls - the link points to a decent article.

For simplicity sake, you can first start by using the synchronous APIs which is basically:

fs.writeFileSync
fs.writeJsonSync

You can find all the details into the links above.

Have fun with bot coding!

marcopeg
  • 1,028
  • 10
  • 15
  • Thank you so much for this it's very detailed and just what i needed to help me along! I am going to use this today and see how i go, i appreciate the time you put in to answer this for me! – Carl Lipthorpe Sep 20 '19 at 07:53
  • You are very welcome, just remember to pass your experience on and always put a some time in helping out other guys :-) – marcopeg Sep 20 '19 at 08:45
  • Hi only me again! I have used the example code and managed to update the array in the JSON file. However at the moment i can only get it to overwrite the JSON file and not append to the end of the array. I am not sure where to go from here.. Any insight would be greatly appreciated! I'm not sure if i need to use for(var i in 'my_array') – Carl Lipthorpe Sep 24 '19 at 08:21
  • The code examples above are very simple, yes, if you follow that your app will end up overriding the file. This is often an acceptable behavior in basic apps or development exercises. If you want to move forward, a simple and valuable piece of experience would be looking into databases. May I suggest Postgres? – marcopeg Sep 24 '19 at 13:01