0

I am new to programming, and I heard that some guys on this website are quite angry, but please don't be. I am creating one web app, that has a web page and also makes som ecalculations and works with database (NeDB). I have an index.js

    const selects = document.getElementsByClassName("sel");
    const arr = ["Yura", "Nairi", "Mher", "Hayko"];
    for (let el in selects) {
      for (let key in arr) {
        selects[el].innerHTML += `<option>${arr[key]}</option>`;
      }
    }

I have a function which fills the select elements with data from an array.

In other file named: getData.js:

    var Datastore = require("nedb");
    var users = new Datastore({ filename: "players" });
    users.loadDatabase();

    const names = [];

    users.find({}, function (err, doc) {
       for (let key in doc) {
          names.push(doc[key].name);
       }
    });

I have some code that gets data from db and puts it in array. And I need that data to use in the index.js mentioned above, but the problem is that I don't know how to tranfer the data from getData.js to index.js. I have tried module.exports but it is not working, the browser console says that it can't recognize require keyword, I also can't get data directly in index.js because the browse can't recognize the code related to database.

yuraArmenian
  • 23
  • 1
  • 6
  • 1
    We need to know about the arch of your web app. Please tell us (confirm) if index.js runs on browser (client side) and getData.js runs on server or cliente side. This will point us to the right direction so we can help you accordingly. – Jone Polvora Apr 19 '20 at 18:22
  • Yes, index.js is a client side file, and getData runs on a server side. – yuraArmenian Apr 19 '20 at 18:29
  • 2
    Do you understand that some of your Javascript runs in the browser and some of it runs on the server? For the Javascript in the web page itself (running in the browser) to get data from the server, it would make an Ajax call, probably using the `fetch()` api in the browser and directed at a specific route on your server that sends the desired data. Or, you could insert the data into the web page when it was originally requested by the browser so the data is already in the web page and your Javascript in the web page can just use it. – jfriend00 Apr 19 '20 at 18:29
  • Related: https://stackoverflow.com/questions/12006417/node-js-server-that-accepts-post-requests. – Daemon Beast Apr 19 '20 at 20:50
  • I think your question is related to javascript modules in general, and how to import and export functions and data. – Jone Polvora Apr 23 '20 at 13:41

2 Answers2

2

You need to provide a server, which is connected to the Database.

Browser -> Server -> DB

Browser -> Server: Server provides endpoints where the Browser(Client) can fetch data from. https://expressjs.com/en/starter/hello-world.html

Server -> DB: gets the Data out of the Database and can do whatever it want with it. In your case the Data should get provided to the Client.


TODOs

Step 1: set up a server. For example with express.js (google it)

Step 2: learn how to fetch Data from the Browser(Client) AJAX GET are the keywords to google.

Step 3: setup a Database connection from you Server and get your data

Step 4: Do whatever you want with your data.

gapsong
  • 41
  • 5
  • The link you provided assumes the OP is using express.js. Also, the link is German. – Daemon Beast Apr 19 '20 at 20:38
  • I assume OP uses no server at all – gapsong Apr 19 '20 at 20:44
  • Express.js is a framework inside Node.js. You assume that the OP is using the framework. Please also provide a link to a tutorial that uses pure Node.js without express.js, such as this one: https://nodejs.dev/ – Daemon Beast Apr 19 '20 at 20:47
  • What do you mean by that? Why should OP use pure Node.js it does not make any sense to use pure Node.js. Furthermore I referenced the starter example. – gapsong Apr 19 '20 at 20:51
  • Why does pure Node.js not make sense? Why should the OP use express.js? Express.js isn't necessary and the OP makes no reference to it, but if you want to keep your answer like that, I have nothing against it. – Daemon Beast Apr 19 '20 at 20:53
0

At first I thought it is a simple method, but them I researched a little bit and realized that I didn't have enough information about how it really works. Now I solved the problem, using promises and templete engine ejs. Thank you all for your time. I appreciate your help)

yuraArmenian
  • 23
  • 1
  • 6