【发布时间】:2021-05-01 22:14:05
【问题描述】:
如何将对象数组保存到 Firestore 并重新加载?我收到此错误
未捕获的 FirebaseError:调用函数 DocumentReference.set() 无效数据。不支持的字段值:自定义对象(在 记录用户书籍/图书馆)
这是我将其保存到 Firebase 并在对 myLibrary 数组进行修改后稍后加载的代码。
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();
【问题讨论】:
标签: javascript firebase google-cloud-firestore