1

When I am trying to loop over app.get with app.post it is only working once. After that data not clear or changed. How to fixed that. I am trying to get user input data and return data from the Nodejs database according to user input data. I also used the Nedb npm package for that

This working fine without any error I want to fix only the iterable issue. Please help me. Thank you

-index.js-

var express = require('express');
var app = express();
var io = require('socket.io')(server);
var bodyParser = require("body-parser");

var Datastore = require("nedb"),
    db = new Datastore({ filename: "database", autoload: true });

app.use(express.static('public'));


var urlencodedParser = bodyParser.urlencoded({ extended: false });

function setup() {
    db.remove({}, { multi: true }, function(err, numRemoved) {
      // clear the database
    });
}

app.post("/new", urlencodedParser, function(request, response) {

    var Y = parseInt(request.body.year);
    var M = parseInt(request.body.month);
    var D = parseInt(request.body.day);

    db.find({ Y: Y, M: M, D: D }, function(err, docs) {
      console.log(docs);

      app.get("/data", function(request, response) {
        response.send(docs);
      });

      // docs = [];

    });
    response.redirect("/");
});
 
app.get("/reset", function(request, response) {
    setup();
    response.redirect("/");
});

var server = app.listen(4000, () => {
    console.log("Listening on port 4000..");
})

io.on('connection', (socket) => {
    console.log("User connected");
})

This HTML file inside the public folder

<!DOCTYPE html>
<html>
  <head>
    <title>Example</title>
    <meta name="description" content="example" />
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body style="text-align:center;">
    <header>
      <h1 style="color:green;">Insert Date</h>
    </header>

    <main>
      <form
        action="/new"  
        method="POST"
      >
        <label for="start">Enter Date :</label>

        <input type="txt" name="year">
        <input type="txt" name="month">
        <input type="txt" name="day">

        <button type="submit">Add</button>

      </form>

      <section class="temp">
        <ul id="temp"></ul>
      </section>

      <button><a href="/reset">Reset</a></button>
    </main>

    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

    <script>
      $(function() {
        $.get("/data", function(data) {
          data.forEach(function(temp) {
            console.log(temp.Temp);
            $("<li></li>")
              .text(temp.Temp)
              .appendTo("ul#temp");
          });
        });
      });
    </script>

  </body>
</html>

This is my database.db file

{"Y":2020,"M":8,"D":11,"Temp":23,"_id":"17qf359PedOU01aT"}
{"Y":2020,"M":5,"D":12,"Temp":24,"_id":"HCWfpZXtN68XTJSN"}
{"Y":2020,"M":5,"D":13,"Temp":45,"_id":"HVg84J7g6hWPmmB3"}
{"Y":2020,"M":4,"D":4,"Temp":56,"_id":"IXE1DpPfuXxaCLit"}
{"Y":2020,"M":3,"D":6,"Temp":23,"_id":"V6Bnk7pngVYnY1Rg"}
{"Y":2020,"M":2,"D":2,"Temp":12,"_id":"cQwrlpurIHWLyR1m"}
  • It makes no sense for your `app.get('/data'...)` handler to be nested inside the callback from your `db.find()` operation. It's your client -- your user's browser -- that accesses your express app with `GET` and `POST` requests. – O. Jones Nov 18 '20 at 14:58
  • Thanks for your answer, –  Nov 18 '20 at 15:22
  • I solved the problem thank your word –  Nov 18 '20 at 15:55

0 Answers0