[英]User Array is undefined after sign up in firebase
I have problem with gettin array during login after creating account.创建帐户后登录期间我遇到了 gettin 数组的问题。
When a user creates an account an entry is also created in users collection with array called Tickets which is empty.当用户创建帐户时,还会在 users 集合中创建一个条目,其中包含名为 Tickets 的数组,该数组为空。
I using this code to do this我使用此代码来执行此操作
const createUserDocument = async (user) => {
if (!user) return;
const userRef = firestore.doc(`users/${user.uid}`);
const snapshot = await userRef.get();
if (!snapshot.exists) {
const { email, displayName } = user;
userRef.set({
email: email,
name: displayName,
tickets: [],
admin: false,
});
}
};
In my component i want show user tasks so here's my component.在我的组件中,我想显示用户任务,所以这是我的组件。
import { useState, useEffect, useContext } from "react";
import { firestore } from "services/firebase";
import { UserContext } from "providers/UserProvier";
// Components
import Modal from "components/Modal/Modal";
import AddNewItem from "components/AddNewItem/AddNewItem";
const TicketsView = () => {
const [visible, setVisible] = useState(false);
const [tickets, setTickets] = useState([]);
const { uid } = useContext(UserContext);
useEffect(() => {
GetUserTickets(uid);
}, [uid]);
const GetUserTickets = (uid) => {
firestore
.collection("users")
.doc(uid)
.onSnapshot((snap) => {
const newTickets = [];
snap.data().tickets.map((ticket) => newTickets.push(ticket));
setTickets(newTickets);
});
};
const ShowModal = () => {
setVisible(!visible);
};
return (
<>
<AddNewItem ShowModal={ShowModal} />
<Modal visible={visible} ShowModal={ShowModal} />
<h2>hello</h2>
</>
);
};
export default TicketsView;
I don't know why useEffect execute two times.我不知道为什么 useEffect 执行两次。
In first execution snap.data()
is undefined and second exection i see info from user doc.在第一次执行
snap.data()
未定义,第二次执行我看到来自用户文档的信息。
I have problem with gettin array during login after creating account.创建帐户后登录期间我遇到了 gettin 数组的问题。
When a user creates an account an entry is also created in users collection with array called Tickets which is empty.当用户创建帐户时,还会在 users 集合中创建一个条目,其中包含名为 Tickets 的数组,该数组为空。
I using this code to do this我使用此代码来执行此操作
const createUserDocument = async (user) => {
if (!user) return;
const userRef = firestore.doc(`users/${user.uid}`);
const snapshot = await userRef.get();
if (!snapshot.exists) {
const { email, displayName } = user;
userRef.set({
email: email,
name: displayName,
tickets: [],
admin: false,
});
}
};
In my component i want show user tasks so here's my component.在我的组件中,我想显示用户任务,所以这是我的组件。
import { useState, useEffect, useContext } from "react";
import { firestore } from "services/firebase";
import { UserContext } from "providers/UserProvier";
// Components
import Modal from "components/Modal/Modal";
import AddNewItem from "components/AddNewItem/AddNewItem";
const TicketsView = () => {
const [visible, setVisible] = useState(false);
const [tickets, setTickets] = useState([]);
const { uid } = useContext(UserContext);
useEffect(() => {
GetUserTickets(uid);
}, [uid]);
const GetUserTickets = (uid) => {
firestore
.collection("users")
.doc(uid)
.onSnapshot((snap) => {
const newTickets = [];
snap.data().tickets.map((ticket) => newTickets.push(ticket));
setTickets(newTickets);
});
};
const ShowModal = () => {
setVisible(!visible);
};
return (
<>
<AddNewItem ShowModal={ShowModal} />
<Modal visible={visible} ShowModal={ShowModal} />
<h2>hello</h2>
</>
);
};
export default TicketsView;
I don't know why useEffect execute two times.我不知道为什么 useEffect 执行两次。
In first execution snap.data()
is undefined and second exection i see info from user doc.在第一次执行
snap.data()
未定义,第二次执行我看到来自用户文档的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.