Adding an API to get the latest ten readings

We want the frontend of our application to get the last ten readings to populate the initial values of the chart. To do this, we're going to add another API to get the readings from the database:

    app.get('/temperature/history', function (req, res) {
      databaseOperations.fetchLatestReadings('temperature', 10, (err, results) => 
{ if (err) { /** * If any error occured, send a 500 status to the frontend and log it */ console.error(err) return res.status(500).end() } /** * Return the reverse of the results obtained from the database. */ res.json(results.reverse()) }) })

An interesting thing to note is that we do not return the results as we get them from the database. Instead, we return the reversed results. This is because the charts accept data in ascending order, whereas we obtain them from the database in descending order due to the limit we put in the query.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.147.190