Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Fetch new data from sqlite database on reload

I managed to get sensor data into a sqlite3 database. Every 2 minutes new values appear in the database. Now I want to display it with chart.js and therefore make use of sql.js The code I use is:

const config_sqljs = {
    locateFile: filename => (
      'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.5.0/sql-wasm.wasm'
    )
}
  
const sqlPromise = initSqlJs(config_sqljs)

const dataPromise = fetch((
'/sensors.db'
))
.then(res => res.arrayBuffer())    
Promise.all([sqlPromise, dataPromise])

.then(([SQL, buf]) => {
const db = new SQL.Database(new Uint8Array(buf))
    //const res = db.exec('select time_,temp,humi  from sensors where topic=\"/x/sensors\"')
    const res = db.exec('select time_,temp,humi from sensors where topic =\"/y/sensors\" and (date=\"29.11.2021\" or date=\"30.11.2021\" or date=\"01.12.2021\");')

    const values = res[0].values;
    let labelx = values.map((arr) => arr[0]);
    let y = values.map((arr) => arr[1]);
    let y2 = values.map((arr) => arr[2]);
})

It works well at first, but then I realized that the values in the arrays labelx, y, and y2 are not the newest one, they lack beheind, about half a day. The new values alwasy appear in the database. I think it has todo something with Promise.all() and .then but unfortunally I don't know much about js. The code is executet each time the site loads or reloads, even I I use Strg+F5 to force a complete, uncached reload it stays the same. Can you help me? Thanks!

=== EDIT ===

I adapted your suggestion:

const dataPromise = fetch((
    '/db.db', {
    method: "GET",
    "cache-control": "no-store"}
))
.then(res => res.arrayBuffer())´

Sadly I get the error:

Uncaught (in promise) Error: file is not a database

the relevant line:

const res = db.exec('select x from db;')

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Fetch might be caching the responses. Try:

fetch('/sensors.db', {
  method: "GET",
  "cache-control": "no-store"
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda