0

So i ve used mongodb watch() and on(change) to detect changes in mongodb database . Now i need to send the changed data to the view ( update variable value after rendering it to the view)

  db.collection("shortenedURLs")
    .find()
    .toArray(function(err, results) {
      let change_streams = db.collection("shortenedURLs").watch();
      change_streams.on("change", function(change) {

        db.collection("shortenedURLs")
          .find()
          .toArray(function(err, results) {
         urls=results // this is the variable 
         }); 
      });
      res.render("./gestion.ejs", { urls: results, user: req.user });
    });
});

but nothing happened ! Is there a way to send data to view without render(view , param) ?

Az Emna
  • 527
  • 2
  • 10
  • 26
  • various methods available to do that are `websocket, long-polling, server-sent events` etc. Read how they work here https://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet – 1565986223 Mar 29 '19 at 10:53
  • 1
    @Neil Lunn I think it might be duplicate to some other question, but definitely not the one you linked to – 1565986223 Mar 29 '19 at 10:55
  • @naga-elixir-jar I think it has more than one duplicate, but it's definitely not the one you think it is. – Neil Lunn Mar 29 '19 at 10:57
  • Come on ! it's not a duplicate ! i ve already checked that post – Az Emna Mar 29 '19 at 10:57
  • it's not for mongodb ! it's with express.js – Az Emna Mar 29 '19 at 10:57
  • But you did not check how to respond to received events. In both the attached posts – Neil Lunn Mar 29 '19 at 10:58
  • i need to know how to update data that i already send in express view – Az Emna Mar 29 '19 at 10:58
  • @Neil agreed, but it would be helpful if you can link to a question where what the OP asked is discussed more in detail. The one you linked does contain something about socket.io but that's hardly a complete solution – 1565986223 Mar 29 '19 at 10:58
  • @naga-elixir-jar The issue is **not** with socket-io. Read the code and then read the change stream/tailable cursor answers and then read about where you can access the returned result of a callback. Look where the `res.render` call is in the question. That's the **useful** information. Duplicate of marked existing answers. – Neil Lunn Mar 29 '19 at 11:00
  • `the problem is that when i try to render i get an error because i cannot set headers after they are sent to the client` <- this sentence? also you could help rephrase the question – 1565986223 Mar 29 '19 at 11:02
  • and maybe link to this https://stackoverflow.com/questions/19995/is-there-some-way-to-push-data-from-web-server-to-browser – 1565986223 Mar 29 '19 at 11:02
  • Thank you for your answer but it's not about socket not even about mongodb ! it's like you have a variable that you already sent to the view with render() and now you just need to change the value of that variable without calling render again ! – Az Emna Mar 29 '19 at 11:04
  • I've tried to make the variable as "global" but it didn't work – Az Emna Mar 29 '19 at 11:04
  • 1
    @Az Emna, read the last link i've put and do more research – 1565986223 Mar 29 '19 at 11:06

0 Answers0