Skip to content
Advertisement

How to store data from a MariaDB in my node.js environment

I have a MariaDB that stores Energy-Data like voltage, frequency and so on. My aim is to visualize the data in a Web-application. Though i achieved to connect the MariaDB to node.js and log the data on a specific port thanks to the code below, i don’t have a clue how to store this data for further mathematic operations or visualizations.

How can i store the data for further operations?

const express = require('express');
const pool = require('./db');
const app = express();
const port = 4999;

// expose an endpoint "persons"
app.get('/persons', async (req, res) => {
    let conn;
    try {
        // make a connection to MariaDB
        conn = await pool.getConnection();

        // create a new query to fetch all records from the table
        var query = "select * from Herget_Netz2_WirkleistungL1";

        // run the query and set the result to a new variable
        var rows = await conn.query(query);

        console.log('Daten kommen');
        

        // return the results
        res.send(rows);
             
    } catch (err) {
        throw err;
    } finally {
        if (conn) return conn.release();
    }
});

app.listen(port, () => console.log(`Listening on pfort ${port}`));

Advertisement

Answer

This question is quite broad.

It sounds like you need to set up a frontend and call fetch on your endpoint, something like:

fetch(<your-url>/persons)
  .then(r => r.json())
  .then(yourData => "<p>" + yourData "</p>")

Your data will be interpolated into HTML then. You will need to iterate over it.

The “storage” will take place in the variable you define in the second .then(yourData) of the promise for you to do further operations on.

You should search for tutorials like “set up frontend with maria db database and node backend”.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement