Uploading images to Firebase - issues and debugging steps
The goal of the project was to upload images to Firebase database and then display it on Glitch.
Things I tested so far:
1. Sending timestamp for database
I tried sending humidity sensor initially. The sensor didn’t work but I eventually send the timestamp to Firebase Realtime Database. See the previous post.
let gpio = require("onoff").Gpio; let firebase = require('firebase'); let app = firebase.initializeApp({ databaseURL: "https://daylight-analysis.firebaseio.com/" }) let humtemp = new gpio(4, 'in'); let n = 0; // console.log(humtemp) // console.log(humtemp._readBuffer.toString()) let database = app.database(); setInterval(() => { database.ref(Date.now()).set({ temperature: n }) console.log(n) n++ }, 2000)
2. Sending images to database storage using Node
This took sometime to resolve. Firebase has multiple products. I had to send two types of data: image and timestamp. I assumed, partly due to how all the Firebase apps are branded, that I can use Realtime Database.
I spoke to Stuti, who used Firebase. She told me to use Storage for data and Realtime Database for the metadata. Firebase is good for for running apps but not images or videos. After a long weekend trying to debug why I couldn’t post an image to Storage, here is the following I figured out:
1. No support for Node: Based on the link ↓↓↓
2. Firebase Storage uses Google cloud as a dependent ↓↓↓
Code that I used based on Firebase Get Started >> Web >> Node along with Firebase Storage >> Web >> Node:
//Initialize on Cloud Functions // The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore. const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount), apiKey: "<INSERT>", authDomain: "<INSERT>", databaseURL: "https://<INSERT>.com", projectId: "<INSERT>", storageBucket: "<INSERT>", messagingSenderId: "<INSERT>", appId: "<INSERT>", measurementId: "<INSERT>" });
// Initialize Firebase // firebase.initializeApp(firebaseConfig); // firebase.analytics();
var storage = firebase.storage(); var storageRef = storage.ref(); var imagesRef = storageRef.child('images'); var database = firebase.database();
//admin.initializeApp();
//nodejs as a FileServer for CRUD //read a file from path and provide standard output //If encoding is specified then this function returns a string. Otherwise it returns a buffer. //ref: https://nodejs.org/en/knowledge/file-system/how-to-read-files-in-nodejs/ fs = require('fs') fs.readFile('/solarlapse/cam.jpg', function (err,data) { if (err) { return console.log(err); } else { console.log(data); let fileName = '1'+'abc'+'.jpg' let mountainsRef = storageRef.child(fileName); let file = data; ref.put(file) .then(function(snapshot) { console.log('Uploaded a blob or file!',snapshot); //snapshot might contain the link to the image //might be a json ---> figure out which filed contains the url // store it in url variable // let url = snapshot.url;
});
***
There might be some code issues since it’s been updated a few times but you will get the gist.
The following image show some of the errors like “firebase storage is not a function” etc.
I went over the Firebase documentation once again and noticed the notes that mentioned about Node support or lack of it.










