简体   繁体   English

如何使用 Node Js 和 Express js 中的渲染更新已发送的数据

[英]How to update already sent data with render in Node Js and Express js

I'm trying to send data at any time from my router.js to be read in Javascript, but I don't know how to pass it without having to render the page again.我试图随时从我的 router.js 发送数据以在 Javascript 中读取,但我不知道如何传递它而不必再次呈现页面。 I want when the condition for the new coordinate is met, the data on the page is updated without reloading the page again.我希望当满足新坐标的条件时,无需再次重新加载页面即可更新页面上的数据。

router.get('/', function (req, res) {

    dbC.db.collection('collection').where('id', '==', 'AnID')
      .onSnapshot(querySnapshot => {
        querySnapshot.docChanges().forEach(change => {
          if (change.type === 'added') {
            let coord = change.doc.data();
            console.log('--------------New coord--------------');
            res.header({ lat: coord.payload_fields.latitude_gps, lng: coord.payload_fields.longitude_gps });
          }
         });
      });
      res.render('index', { title: "FMCS", title2:"Tracing", active: {home: true, map: true},  lat: 0, lng: 0 });
    })


My hbs:
 
<script>
  var point = { lat: {{{lat}}}, lng: {{{lng}}} };
</script>


  

You should use Firestore in the rendered page and not in the express controller if you want realtime data updates in the web page.如果您想在网页中实时更新数据,您应该在呈现的页面中使用 Firestore,而不是在 express 控制器中。

Firstly add Firestore dependecy in the rendered page heading首先在呈现的页面标题中添加 Firestore 依赖项

<script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-firestore.js"></script>

Secondly initialize Firestore in the rendered page and listen to updates其次在呈现的页面中初始化 Firestore 并监听更新

<script>
  firebase.initializeApp({
    apiKey: '### FIREBASE API KEY ###',
    authDomain: '### FIREBASE AUTH DOMAIN ###',
    projectId: '### CLOUD FIRESTORE PROJECT ID ###'
  });

  var db = firebase.firestore();

  dbC.db.collection('collection').where('id', '==', 'AnID')
  .onSnapshot(querySnapshot => {
    querySnapshot.docChanges().forEach(change => {
      if (change.type === 'added') {
        // use DOM API to update data location in the rendered page
      });
    });
  })
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM