node.js - 使用vuejs和nodejs从mongodb数据库读取数据

标签 node.js express vue.js vuejs2 vue-component

我正在创建一个 Web 应用程序来实时可视化我房间的温度。目前我用Raspberry读取该值,然后加载数据库Mongodb。现在要在浏览器上实时显示它,我该怎么做?我将 Node.js 和 vue.js 与 Express 一起使用。如何实时将值传递给Vue.js?

var App = Vue.component('App',{
    template: "<h1> {{title}} </h1>",
    data() {
    
        let test= "hello";
        return {title: test};
    }
});

new Vue({
    el:"#app"
});
<div id="app">
   <App></App>
</div>

最佳答案

你的后端代码应该是这样的:

//get the value from db
//create a variable tmp that will receives temperature from db
let tmp;


var router = express.Router();
router.get('/temperature', function(req, res) {
  res.json({
    temperature: tmp
  });
});


app.use('/api', router);

在前面您可以访问该 api :

localhost:8080/api/temperature

并使用 axios您可以调用您的后端并实时返回温度

var App = Vue.component('App', {
  template: "<h1> {{temperature}} </h1>",
  data() {


    return {
      temperature: 0
    };
  },
  created: function() {

    this.fetchTemp('api/temperature');

    setInterval(()=> {
      this.fetchItems('api/temperature');
    }, 500);
  },

  methods: {

    fetchTemp(uri) {

      axios.get(uri).then((res) => {
        this.temperature = res.data.temperature;
      });
    },
  }
});

I tried to simulate your use case by getting the current time from REST API and show it every second 

new Vue({
  el: '#app',
  data() {
    return {
      now: 0
    };
  },
  created: function() {

    this.fetchTemp('https://script.googleusercontent.com/a/macros/esi.dz/echo?user_content_key=ypoXRw1nVHj-h1VRDmh6TXSI1VpIPWW7Qo2n9El6RqoxAJ3v28nBI9bDY_4UAE0TQJ3pSozxpbTiRvFpmD8pvcTkGSnPAtgRm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_nRPgeZU6HP_B2BW4qWwVPUuHIcJ3mEdrfLIfNZsYUQi0c--vxV_3BX606CngcowlqSfFH8SSiqMPrUuXDMsd72r-P39_jlVDMh0BMLnMwXU02UuEHWiuob4ULL2SJgrtyBAf43AAwP8&lib=MwxUjRcLr2qLlnVOLh12wSNkqcO1Ikdrk');

    setInterval(() => {
      this.fetchTemp('https://script.googleusercontent.com/a/macros/esi.dz/echo?user_content_key=ypoXRw1nVHj-h1VRDmh6TXSI1VpIPWW7Qo2n9El6RqoxAJ3v28nBI9bDY_4UAE0TQJ3pSozxpbTiRvFpmD8pvcTkGSnPAtgRm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_nRPgeZU6HP_B2BW4qWwVPUuHIcJ3mEdrfLIfNZsYUQi0c--vxV_3BX606CngcowlqSfFH8SSiqMPrUuXDMsd72r-P39_jlVDMh0BMLnMwXU02UuEHWiuob4ULL2SJgrtyBAf43AAwP8&lib=MwxUjRcLr2qLlnVOLh12wSNkqcO1Ikdrk');

    }, 1000);
  },

  methods: {

    fetchTemp(uri) {

      axios.get(uri).then((res) => {
        this.now = new Date(res.data.fulldate).toLocaleString();


      }).catch(err => {});
    }
  }
})
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
  <div id="app">
    <h1> Now : {{now}} </h1>
  </div>
</body>

</html>

关于node.js - 使用vuejs和nodejs从mongodb数据库读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249384/

相关文章:

javascript - 如何向实际的 post 请求添加额外的 req.body 属性,然后以 Express 方式重定向它

javascript - 为什么我的回调函数不运行?

vue.js - 框架 7 Vue2 : Error in Mounted Hook for this. $f7

node.js - TypeError : Grid is not a constructor. Mongodb Node 驱动

node.js - 在 MongoDB 中删除早于 10 分钟的记录?

javascript - node.js 函数回调

reactjs - 使用 Express 后端将 create-react-app 部署到 heroku 在浏览器中返回无效的主机 header

vue.js - 在 vue.js 应用程序中的何处存储静态内容(例如国家/地区代码)?

vue.js - 防止在 IOS ionic 5 Vue 应用程序上向后滑动

javascript - Node JS : get number of files in a directory