Read data from a mongodb database using vuejs and nodejs(使用 vuejs 和 nodejs 从 mongodb 数据库中读取数据)
问题描述
我正在创建一个 Web 应用程序来实时显示我房间的温度.目前我用 raspberry 读取值,然后加载数据库 Mongodb.现在要在我的浏览器上实时显示它,我该怎么做?我将 Node.js 和 vue.js 与 express 一起使用.如何将值实时传递给 Vue.js?
I'm creating a web application to visualize in real time the temperature of my room. Currently I read the value with raspberry and then load the database Mongodb. Now to display it in real time on my browser how do I do it? I'm using Node.js and vue.js together with express. How do I pass the value to Vue.js in real time?
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>
推荐答案
你的后端代码应该是这样的:
Your code in backend should be like this :
//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:
in the front you have access to that api :
localhost:8080/api/温度
localhost:8080/api/temperature
使用 axios 您可以调用后端并取回温度实时
And using axios you could make call to your backend and get back the temperature in real time
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="aHR0cHM6Ly91bnBrZy5jb20vdnVlQDIuNS4xNy9kaXN0L3Z1ZS5qcw=="></script>
<script src="aHR0cHM6Ly91bnBrZy5jb20vYXhpb3MvZGlzdC9heGlvcy5taW4uanM="></script>
<script src="aHR0cHM6Ly91bnBrZy5jb20vdnVlLWF4aW9zQDIuMS40L2Rpc3QvdnVlLWF4aW9zLm1pbi5qcw=="></script>
</head>
<body>
<div id="app">
<h1> Now : {{now}} </h1>
</div>
</body>
</html>
这篇关于使用 vuejs 和 nodejs 从 mongodb 数据库中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 vuejs 和 nodejs 从 mongodb 数据库中读取数据


- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01