简体   繁体   English

Firestore v9 预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore

[英]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.jsSignup.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.

相关问题 预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore - Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore Cloud Functions:collection() 的第一个参数应为 CollectionReference、DocumentReference 或 FirebaseFirestore - Cloud Functions: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore 如何访问 firebase FirebaseError:预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore - How can I access firebase FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore 我该如何解决 FirebaseError:collection() 的预期第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore - How can i solve FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore 有人可以帮我吗 - FirebaseError:collection() 的第一个参数应该是 CollectionReference、DocumentReference 或 FirebaseFirestore? - Can someone help me - FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore? Firestore 集合到具有 Firebase v9 的对象数组 - Firestore collection to array of objects with Firebase v9 CollectionReference 到 DocumentReference - CollectionReference to DocumentReference 如何对 Firestore v9 进行分页 - How to Paginate Firestore v9 React 和 Firebase Firestore V9 上一页分页返回“第一页” - React and Firebase Firestore V9 Previous Page Pagination returning to "first page" Firestore v9 创建/更新子集合 - Firestore v9 create/update subcollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM