Firestore v9 预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore
Firestore v9 Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
我尝试将新用户从注册页面添加到 firestore,但没有成功。
这是我的 config.js
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getDatabase } from "firebase/database";
const firebaseConfig = initializeApp ({
//cred
});
export const auth = getAuth(firebaseConfig)
export const database = getDatabase(firebaseConfig);
和Signup.js
import React, { Component } from "react";
import firebase from "./config";
import { collection, addDoc,doc } from "firebase/firestore";
class Signup extends Component {
constructor(props) {
super(props);
this.state = {
name: '-',
email: '-',
};
}
addUser = e => {
e.preventDefault();
try {
const collectionRef = doc(database, 'users');
const docRef = addDoc(collectionRef, {
name: this.state.name,
email: this.state.email,
});
} catch (e) {
console.error("Error adding document: ", e);
}
render(){
return (
<div>
//do something
<button className="btn btn-primary" onClick={this.addUser}>Submit</button>
</div>
)}
}
这是错误:
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
我把Config.js改成了:
export const auth = getAuth()
export const database = getDatabase();
我仍然遇到同样的错误。
是不是说明我没有连接firestore成功? :'(
请帮我解决这个问题。
谢谢。
const collectionRef = doc(database, 'users');
这里的database
是实时数据库实例,不是Firestore。
export const database = getFirestore(); // not getDatabase()
您必须使用 getFirestore()
获取 Firestore 实例并将其传入 doc()
。
即使向 doc()
.
传递了无效参数,错误也会显示 Expected first argument to collection()
尝试更改集合的路径
const collectionRef = doc(database, '/users');
它对我有用!
我尝试将新用户从注册页面添加到 firestore,但没有成功。
这是我的 config.js
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getDatabase } from "firebase/database";
const firebaseConfig = initializeApp ({
//cred
});
export const auth = getAuth(firebaseConfig)
export const database = getDatabase(firebaseConfig);
和Signup.js
import React, { Component } from "react";
import firebase from "./config";
import { collection, addDoc,doc } from "firebase/firestore";
class Signup extends Component {
constructor(props) {
super(props);
this.state = {
name: '-',
email: '-',
};
}
addUser = e => {
e.preventDefault();
try {
const collectionRef = doc(database, 'users');
const docRef = addDoc(collectionRef, {
name: this.state.name,
email: this.state.email,
});
} catch (e) {
console.error("Error adding document: ", e);
}
render(){
return (
<div>
//do something
<button className="btn btn-primary" onClick={this.addUser}>Submit</button>
</div>
)}
}
这是错误:
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
我把Config.js改成了:
export const auth = getAuth()
export const database = getDatabase();
我仍然遇到同样的错误。 是不是说明我没有连接firestore成功? :'( 请帮我解决这个问题。 谢谢。
const collectionRef = doc(database, 'users');
这里的database
是实时数据库实例,不是Firestore。
export const database = getFirestore(); // not getDatabase()
您必须使用 getFirestore()
获取 Firestore 实例并将其传入 doc()
。
即使向 doc()
.
Expected first argument to collection()
尝试更改集合的路径
const collectionRef = doc(database, '/users');
它对我有用!