如何将 Vue 应用程序中的 Influxdb 数据与 node.js 连接?

How to connect Influxdb data in Vue app with node.js?

我有一个 InfluxDB,我想连接到我的 vue 应用程序。我已经连接到数据库,并且可以使用以下代码在终端(不是控制台)中记录数据:

// index.js
import express from "express";

// These lines make "require" available
import { createRequire } from "module";
const require = createRequire(import.meta.url);

import store from '../store/index.js'
store.getters.config
//Initialize the Client-----------------------------------
const {InfluxDB, flux} = require('@influxdata/influxdb-client')
const url = 'http://193.174.28.232:5102';
const token = 'qE83Rq0yQPGek6teUu745OkrOKW7jmInL5QrMq48-VIaXOagxPP3B8fvATAZsi7avaOlOSuMI0lRAKY9h9hnxg=='
const org = 'TeamEE'
const bucket = 'fdre818'
const client = new InfluxDB({url: url, token: token})
const o = [] 
const speed = []

//Execute a Flux query---------

const queryApi = client.getQueryApi(org)
const query = flux`from(bucket: "fdre818") 
  |> range(start: 2022-04-20T10:00:00Z, stop: 2022-04-20T10:02:00Z)
  |> filter(fn: (r) => r._measurement == "86B20CC8")
  |> filter(fn: (r) => r._field == "Speed")
  |> aggregateWindow(every: 5s, fn: mean)
  |> map(fn: (r) => ({ r with _value: r._value * 3.6 }))
  |> limit(n: 10)`
queryApi.queryRows(query, {
    next(row, tableMeta) {
        const o = tableMeta.toObject(row)
        speed.push(o._value)
    
    },
    error(error) {
        console.error(error)
        console.log('Finished ERROR')
    },
    complete() {
        console.log('Finished SUCCESS')
        store.state.suppliedInflux.speed =  speed;
    },
})

export {client}

现在我想将该数据获取到我的 vue 组件,但它不起作用。我试图导入 influxdb.js 文件 import * as Influxdb from '../database/influxdb.js' 这给了我错误:

解决方案是:

刚刚导入@influxdata/influxdb-client-browser 代替@influxdata/influxdb-client。有关详细信息,请参阅 influxdb-client-js 安装。