[英]Firebase updateDoc function not working [Web v9 (modular)]
[英]Modular usage of Firebase v9 Beta Library
其他重要信息:
这是我的代码在 v8 上通常的样子:
// firebase.js
import firebase from "firebase/app";
import "firebase/firestore";
export const app = firebase.initializeApp(config);
export const firestore = app.firestore();
// app.js
import { firestore } from './firebase'
firestore.collection("users").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
});
});
我相信在 v9 中,Firebase 不再有这种副作用导入问题,所以现在我们明确说明我们想要从包中获得哪些功能。
// firebase.js
import { initializeApp } from 'firebase/app'
import { getFirestore } from 'firebase/firestore'
export const app = initializeApp(config)
export const firestore = getFirestore()
下面的代码来自Firebase 文档
// app.js
import { addDoc, collection } from "firebase/firestore";
try {
const docRef = await addDoc(collection(db, "users"), {
first: "Alan",
middle: "Mathison",
last: "Turing",
born: 1912
});
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
将上面的代码(使用addDoc
和collection
进口)指的是初始化实例的FireStore在firebase.js
,还是有使用初始化实例的FireStore有道?
Firestore 实例由核心 Firebase 应用程序管理,firestore 模块仅包含发出请求的库。 无需引用核心应用即可获取 Firestore 实例的解决方案是从 Firestore 模块请求实例。
import { getFirestore } from 'firebase/firestore';
const db = getFirestore();
来源: https : //firebase.google.com/docs/firestore/quickstart#initialize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.