In the past I have been successful integrating a p5 sketch with a firebase realtime database. When I went to start a new project, it’s not working the way that it used to and I am stuck.
With firebase version 8, I linked to the firebase library almost the same way I link to the p5 library, with a script tag in index.html:
This doesn’t work in the latest version of firebase.
The firebase installation instruction tells me to add the script as a type = “module” and then to add these lines of code into my index.js file:
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.1.0/firebase-app.js';
import { getDatabase } from "https://www.gstatic.com/firebasejs/10.1.0/firebase-database.js";
my sketch.js file doesn’t run properly if I call it as type=module, and I can’t get my p5 code to work in index.js. If I run both index.js and sketch.js from index.html, sketch.js is not able to access the database variables defined in index.js (even though I believe they are global).
Here is the link to the full project : p5.js Web Editor
I’m adding the p5 editor link because it’s easy to see, but I’ve mainly been working on it in VScode with a local server, so I know that the error isn’t caused by an issue within the editor.
Thank you! This example was super helpful and I was able to connect with firebase.
I continue to struggle with the syntax since the firebase ref() method used to work but doesn’t now. I think that maybe I am missing a step.
Uncaught TypeError: Cannot read properties of undefined (reading ‘ref’) at _main.default.Element.submitName
my code with ref:
var r = database.ref('name');
vs ref method in firebase reference:
export declare function ref(db: Database, path?: string): DatabaseReference;
So you’re gonna need to import it alongside function getDatabase(), which also belongs to that module as well: import { getDatabase, ref } from "blah-blah-blah.js" globalThis.ref = ref;