node.js - 物联网数据数组的 mongoose.Schema 并将数据提取到 chart.js
问题描述
首先,我需要一个用于 iotdata 的 mongoose.Schema,其中包含:
- 设备ID
- 实体类型
- 时间戳
- 属性有一个键和一个值,例如“键”:“温度”和“值”:12
我写了这个数组,但我不确定,如果它是正确的:
const DataSchema = mongoose.Schema({
device_id:{
type: String,
required: true
},
entity_type: {
type: String,
required: true
},
time: {
type: Date,
required: true
},
attributes: { type : Array , "default" : []}
});
下一行:
const Data = module.exports = mongoose.model('Data', DataSchema);
之后我想将数据提取到 chart.js 前端是用 Angular 编写的。我在 Angular 中创建了一个服务组件来连接到后端 Node.js Express:
export class DataComponent implements OnInit {
data: Array<Data> = [];
data: Data;
device_id: string;
entity_type: string;
time: date,
attributes: array
constructor(private dataService: DataService) { }
ngOnInit(): void {
this.DataService.getData().subscribe(
(data: any) => // set response type to any
{
this.data = data;
console.log(this.data);
}
);
}
}
但我怎样才能将数据提取到chart.j?
var myLineChart = new Chart(ctx, {
type: 'line',
data: data, **-> The data here should be the data from the mongodb**
options: options
});
解决方案
您的模型创建 ID 正确,
您需要创建控制器文件和路由以在 Node JS 中获取数据,发布到您将能够获取数据。请让我知道需要更多帮助
推荐阅读
- css - CSS 转换在 Mozilla 浏览器中不起作用
- kivy - 如何创建高度可调的 KivyMD 卡
- java - 带有 WebClient 的 Cron 调度程序
- php - PHP替换特定字符串后的值
- laravel - 在 laravel 中使用多对多关系同步:PostgreSQL 数据透视表不更新
- c++ - 无法编译包括文件系统的 C++ 程序
- eclipse - Tomcat 9/Eclipse 2020-12:HTTP 状态 404
- python-3.x - 当我使用 linux penguin 在 chromebook 上安装 python 3.9 时,我仍然看到旧版本
- react-native - 在 react native 中单击按钮上的 json 对象生成 CSV 文件
- c++ - 无限循环的编译器优化