![](/img/trans.png)
[英]Expected first argument to collection() to be a CollectionReference, a DocumentReference or 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:collection() 的第一个参数应为 CollectionReference、DocumentReference 或 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');
它对我有用!
导入错误。 我的错误是:
从“firebase/firestore/lite”导入 { 集合,getDocs }; 这在 lite import { getFirestore } from "firebase/firestore"; 这完成了
las importaciones deben ser iguales 进口必须相等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.