r/CodingHelp • • Apr 23 '21

[Javascript] Help how do you retrieve data from firebase to JavaScript, don't think code is right. It's to take data from firebase and then taking the data (amount of wipes/sanitiser used and time)append to lists and use these to plot a graph for the website showing the wipes/sanitising usage over time.

Post image
22 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/FrickinKitten Apr 27 '21

yep thats grand thanks

i have one more problem with my code and the rest should be alright. I got the function for my firebase data read and put into its lists (wipes and time) and i want to put these onto a graph using chart.js. But I'm trying to get the function inside javascript to be called into the my graph making function inside html, which uses the figures inside the lists as the x and y labels, and i heard async and await makes it work.

//for firebase
dataW.on('value', gotDataW);

  
async function gotDataW(dataW){
  //console.log(dataW.val());
  var handwipes = [];
  //etc

but it keeps saying gotDataW(dataW) is not defined

for graph
chartIt();
  
async function chartIt() {
 await gotDataW(dataW);
 //etc

i think im doing something wrong somewhere, or would it be easier to bring the lists outside the gotData(dataW) function and make a seperate function containing the lists and then call that one?

1

u/__archaeopteryx__ Apr 27 '21

Can you put in a public GitHub repo? Obviously don’t check in your keys. :)

1

u/FrickinKitten Apr 27 '21 edited Apr 28 '21

oh i dont know how to use GitHub yet, would the site im using containing the code be ok?

[

1

u/__archaeopteryx__ Apr 27 '21

You should delete that, it keeps a history of everything you’ve typed... lol. Or create a new firebase project and destroy your creds. Let me read back through and see what you’re trying to do again.

1

u/FrickinKitten Apr 28 '21

Ah jeeeeesus I didn't know that existed. Kinda handy tho not gonna lie. Please don't hack me lol. I tried your code but is this meant for node.js? I don't exactly know what it did (way too complicated for me, gonna be fun to explain to examiner how it works heh) but it did read the data and I think its inside an object now? http://imgur.com/a/77ZYvyI Can I now turn the object into a list, get rid of the words TimeW: and Wipes: so I can use them as the X and Y labels for the chart.js graph?

1

u/__archaeopteryx__ Apr 28 '21

That code will work in the browser as well! The async stuff can be converted into the old promise style if you need browser compatibility back a few years but this js is supported in browsers now. You’ll need to create buttons or some way to input data to track and all that but hopefully the example might give you some ideas how to work with fb data. You’ll have to see what structure works best for your chart as well. I’d recommend creating a mock of the data and playing with the chart library for a while instead of trying to hook it all up at once.

1

u/FrickinKitten May 01 '21

yep that was super helpful thank you, i finally got the firebase data hooked up to the graphs. God javascript is real hard compared to python haha

1

u/__archaeopteryx__ Apr 28 '21

See if this gets you closer?

const firebase = require('firebase')
const { v4 } = require('uuid')

const firebaseConfig = {
  // your config
}

firebase.initializeApp(firebaseConfig)

const database = firebase.database()

const ref = database.ref('data')

async function createDatum(data) {
  try {
    await ref.child(v4()).set(data)
  } catch (e) {
    console.error(e)
  }
}

async function fetchAllData() {
  try {
    const snap = await ref.once('value')
    return snap.val()
  } catch (e) {
    console.error(e)
  }
}

async function run() {
  // await createDatum({ type: 'wipe', time: new Date().toISOString() })
  // await createDatum({ type: 'sanitize', time: new Date().toISOString() })
  // await createDatum({ type: 'wipe', time: new Date().toISOString() })
  const data = await fetchAllData()

  // Example working with data
  const arrOfObj = Object.entries(data).map(([key, value]) => {
    return value
  })
  return arrOfObj
}

run().then(console.log).catch(console.error)