Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

0

Fetch might be caching the responses. Try:

fetch('/sensors.db', {
  method: "GET",
  "cache-control": "no-store"
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!