Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

0

Fetch might be caching the responses. Try:

fetch('/sensors.db', {
  method: "GET",
  "cache-control": "no-store"
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda