How do I save an array of objects to Firestore and load it back? I get this error
Uncaught FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: a custom object (found in document userBooks/library)
This is my code to save it to Firebase and to load it later on after modifications have been done to myLibrary array.
let myLibrary = [];
let firestore = firebase.firestore();
let docRef = firestore.doc("userBooks/library");
saveLibrary = function () {
docRef
.set({
userLibrary: myLibrary,
})
.then(function () {
console.log("Library saved!");
})
.catch(function (error) {
console.log("Got an error: ", error);
});
};
saveLibrary()
getUpdate = function () {
docRef.onSnapshot(function (doc) {
if (doc && doc.exists) {
const myData = doc.data();
myLibrary = myData.userLibrary;
console.log(myData.userLibrary);
}
});
};
// Here I add multiple objects to the array myLibrary.
saveLibrary();
getUpdate();