【发布时间】:2021-04-12 09:12:12
【问题描述】:
我在尝试更新 Cloud Firestore 中的数组时收到以下错误消息:
TypeError: Cannot read property 'arrayUnion' of undefined
这是我的代码以及我的 firebase 配置:
import fire from '../config/Firebase';
let tweetToUpdate = 'example-value';
fire.firestore().collection("liked-tweets")
.doc(userID)
.update({
tweets: fire.firestore.FieldValue.arrayUnion(tweetToUpdate),
});
Firebase.js
import firebase from "firebase";
const CONFIG = {
apiKey: "",
authDomain: "twitter-clone-c1b85.firebaseapp.com",
projectId: "twitter-clone-c1b85",
storageBucket: "twitter-clone-c1b85.appspot.com",
messagingSenderId: "987719332990",
appId: "1:987719332990:web:2fde4fb30d70bcf0bb542f",
measurementId: "G-T347VWPPJM",
};
const fire = firebase.initializeApp(CONFIG);
export default fire;
根据错误消息,它表明“fire.firestore”对象未定义,因此没有名为“FieldValue”的属性。但是,我的“火”应该没有“火库”对象吗?
fire.firestore()、fire.storage() 等其他方法工作正常。
我怀疑这与我导出/导入 firebase 的方式有关,但我不知道哪里出错了。
我尝试了一些相关的 SO 线程,例如 Firestore arrayUnion,但没有找到有效的答案。
这是我一直关注的文档:Update elements in an array
任何帮助将不胜感激!!谢谢。
【问题讨论】:
-
如果你能发布整个错误堆栈会更容易。您的代码中没有
arrayUnion,但我们可以通过整个堆栈找到错误源自您的代码的位置。 -
@MinusFour 嘿,感谢您的回复。错误堆栈从我的第一个代码块的第 8 行开始。那里有一个 arrayUnion 方法。对不起,如果我误解了你的问题。
-
所以我猜你正在检索的文档有问题。也许它不存在?
-
@MinusFour 我设法找到了解决方案,见下文。感谢您的观看!
标签: javascript firebase google-cloud-firestore