【发布时间】:2022-06-15 21:43:57
【问题描述】:
我正在使用带有 firebase/firestore 的 react,我正在从 Angular 迁移到 React,但我正在努力在没有 AngularFirestore 的情况下访问 firestore 数据库。我能够登录到 Firestore 并获取 user.uid,我只是无法访问嵌套在每个 user.uid 内的“pred”集合,因此这些数据仅供用户使用。
在 Angular 中,我像这样访问我的数据库:(它有效)
this.auth.user.pipe(take(1)).subscribe((user) => {
if (user) {
this.items = this.db
.collection("users")
.doc(this.user.uid)
.collection("pred")
.valueChanges();
在反应中,我正在尝试做相同的集合、文档、集合,但我找不到明确的文档如何做到这一点。
我的反应尝试:
import React, { useEffect, useState } from "react";
import { useAuthState } from "react-firebase-hooks/auth";
import { useNavigate } from "react-router-dom";
import "./Dashboard.css";
import { auth, db, logout } from "../../services/firebase/firebase-auth";
import { query, collection, getDocs, where, doc } from "firebase/firestore";
function Dashboard() {
const [user, loading, error] = useAuthState(auth);
const [name, setName] = useState("");
const navigate = useNavigate();
const fetchData = async () => {
const userId = auth.currentUser?.uid;
const docRef = collection(db, "users");
const predictions = collection(db, "pred");
const predSnapshot = await getDocs(predictions);
const predList = predSnapshot.docs.map((doc) => doc.data());
return predList;
};
如何返回 predList 如下:
.collection("users")
.doc(this.user.uid)
.collection("pred")
路径顺序,但是使用 react 还是纯 javascript?我正在使用这个 import { query, collection, getDocs, where, doc } from "firebase/firestore"; 包,但我不知道该怎么做。
【问题讨论】:
-
您不必从“pred”集合中获取文档吗?在 dart/flutter 中,我们使用
.snapshots()来获取数据的实时更新
标签: javascript reactjs firebase google-cloud-firestore redux-firestore