[英]Firestore v9 Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
I tried to add new users from the signup page to firestore but it didn't work.我试图将新用户从注册页面添加到 firestore,但没有成功。
Here's my config.js这是我的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);
And Signup.js和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>
)}
}
And here's the error:这是错误:
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore FirebaseError:collection() 的第一个参数应为 CollectionReference、DocumentReference 或 FirebaseFirestore
I changed Config.js to:我将Config.js更改为:
export const auth = getAuth()
export const database = getDatabase();
and I still got the same error.我仍然遇到同样的错误。 Does it mean that I didn't connect firestore successfully?是不是说明我没有连接firestore成功? :'( Please help me solve this problem. Thank you. :'( 请帮我解决这个问题。谢谢。
const collectionRef = doc(database, 'users');
The database
here is an instance of realtime database and not Firestore.这里的database
是实时数据库的一个实例,而不是 Firestore。
export const database = getFirestore(); // not getDatabase()
You must use getFirestore()
to get an instance of Firestore and pass it in doc()
.您必须使用getFirestore()
来获取 Firestore 的实例并将其传递给doc()
。 The error says Expected first argument to collection()
even if an invalid argument is passed to doc()
.即使将无效参数传递给doc()
,该错误也会显示Expected first argument to collection()
。
Try changing the path for the collection尝试更改集合的路径
const collectionRef = doc(database, '/users');
it worked with me!它对我有用!
Error in import.导入错误。 My error is:我的错误是:
import { collection, getDocs } from "firebase/firestore/lite";从“firebase/firestore/lite”导入 { 集合,getDocs }; this in lite import { getFirestore } from "firebase/firestore";这在 lite import { getFirestore } from "firebase/firestore"; and this complted这完成了
las importaciones deben ser iguales imports must be equal las importaciones deben ser iguales 进口必须相等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.