I am using Firebase SDK's
within Node-red (as specified in NPM docs they can be used for IoT devices with NODE.js).
- I can use all of the CRUD methods with
Firebase RealtimeDatabase
. - With
Firebase Firestore
I can only useREAD
andDELETE
functionality. -
SET
andUPDATE
results in weird errors that I couldn't find answers
anywhere on the internet.
I am importing Firebase SDK's
through require()
inside settiings.js
and functionGlobalContext
so I can access them in Node-red functions:
functionGlobalContext: {
firebase: require('firebase/app'),
firebaseDatabase: require('firebase/database'),
firebaseFirestore: require('firebase/firestore'),
// os:require('os'),
// jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false})
},
First I initialize my whole Firebase project with this code (and everything initializes fine without errors):
//Load data from Global contexta
const firebase = global.get('firebase');
const firebaseDatabase = global.get('firebaseDatabase');
const firebaseFirestore = global.get('firebaseFirestore');
const firebaseConfig = {
//my Firebase credentials
};
//Set up Firebase
const app = firebase.initializeApp(firebaseConfig);
const database = firebaseDatabase.getDatabase();
const firestore = firebaseFirestore.getFirestore();
//Save the database reference to Global context
global.set('app', app);
global.set('database', database);
global.set('firestore', firestore);
And here I am trying basic SET
operation with Firestore:
const ft = global.get('firebaseFirestore');
const firestore = global.get('firestore');
const frankDocRef = ft.doc(firestore, "users", "frank");
await ft.setDoc(frankDocRef, {
name: "Frank",
age: 12
});
Unfortunately even though this code is ctrl+c
ctrl+v
from Firestore docs I get this error:
"FirebaseError: [code=invalid-argument]: Function setDoc() called with invalid data. Data must be an object, but it was: a custom Object object (found in document users/frank)"
- When I use the same code inside a web app everything works fine.
- There has to be something going on under the hood with Node-red
- I tried creating the object using various methods and all of them resulted in the same error.
Does anybody have any idea what could be going wrong here?
It seems that Node-red is somehow passing to the setDoc function mutated
object. Or does anybody know how to create object
and not Object object
? I always though that those two things are the same.