简体   繁体   English

如何在 firebase react js 中向经过身份验证的用户添加集合?

[英]How do I add collections to authenticated user in firebase react js?

I am trying to add a collection to the authenticated user by the uid.我正在尝试通过 uid 向经过身份验证的用户添加一个集合。 I am storing the uid with useState but when I pass it to the the db call it return "".我使用 useState 存储 uid,但是当我将它传递给 db 时,调用它返回“”。 If i hard code something in there it works fine.如果我在那里硬编码一些东西,它工作正常。 In the console log, I can see the uid in the state but upon submit, it submits "" I am new to react so any info would be greatly appreciated.在控制台日志中,我可以看到状态中的 uid,但在提交时,它提交“”我是新来的反应所以任何信息将不胜感激。

import React, { useState } from 'react';

import firebase from 'firebase';
import { db } from '../../firebase';

import Button from '@material-ui/core/Button';

import './SignUp.css';

function SignUp({ history }) {
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [checkPassword, setCheckPassword] = useState('');
    const [userId, setUserId] = useState('');

    console.log(userId);

    const handleSignUp = (e) => {
        e.preventDefault();

        if (password !== checkPassword) {
            return alert('Passwords do not match');
        }

        if (!password) {
            return alert('Please enter a password1');
        }

        firebase
            .auth()
            .createUserWithEmailAndPassword(email, password)
            .then((user) => {
                setUserId(user.user.uid);

                db.collection('Users').doc(userId).set({
                    email: user.user.email
                });

                alert('User Created');
                setEmail('');
                setPassword('');
                setCheckPassword('');
                history.push('/');
            })
            .catch((error) => alert(error));
    };

    return (
        <div className="signup">
            <div className="signup__title">Sign Up</div>
            <form onSubmit={handleSignUp}>
                <div className="signup__card">
                    <div className="signup__cardLabel">Email</div>
                    <div className="signup__cardInput">
                        <input
                            type="text"
                            placeholder="username"
                            value={email}
                            onChange={(e) => setEmail(e.target.value)}
                        />
                    </div>
                    <div className="signup__cardLabel">Password</div>
                    <div className="signup__cardInput">
                        <input
                            type="password"
                            placeholder="password"
                            value={password}
                            onChange={(e) => setPassword(e.target.value)}
                        />
                    </div>
                    <div className="signup__cardLabel">Check Password</div>
                    <div className="signup__cardInput">
                        <input
                            type="password"
                            placeholder="password"
                            value={checkPassword}
                            onChange={(e) => setCheckPassword(e.target.value)}
                        />
                    </div>
                    <div className="signup__button">
                        <Button
                            type="submit"
                            variant="contained"
                            color="primary"
                        >
                            Submit
                        </Button>
                    </div>
                </div>
            </form>
        </div>
     );
   }

  export default SignUp;

setUserId(userId) is async so it might not be finished when you run db.collection('Users').doc(userId).set() . setUserId(userId)是异步的,所以当你运行db.collection('Users').doc(userId).set()时它可能不会完成。

And in your case (at least the code I can see), you don't need to put your userId in your state.在你的情况下(至少我能看到的代码),你不需要把你的 userId 放在你的状态。

Just run db.collection('Users').doc(user.user.id).set() instead.只需运行db.collection('Users').doc(user.user.id).set()代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查用户是否在Firebase和Express / Node.js中通过了身份验证? - How to check if user is authenticated in Firebase and Express/Node.js? 如何为预期创建的文档增加价值? (Firebase + 反应) - How do I add value to a document that is expected to be created? (Firebase + React) firebase在创建用户时添加用户信息(react.js) - firebase add user information while creating user (react.js) 我如何在 firebase 中使用 React 收集 map 所有 collections? - How can i map all collections inside of collection in firebase with React? 如何从 angular 中的 firebase 中删除经过身份验证的用户 - How to delete authenticated user from firebase in angular 如何使用我添加的外部脚本来反应 JS? - How do I use external script that I add to react JS? 如何在 React.js 中添加指向这些按钮的链接 - How do I add links to these buttons in React.js 如何在 React JS 上添加输入数字的增量 - How do I add an increment of input number on React JS 如何将文件从表单添加到 Firebase 数据库? Vue js + Vuetify + Firebase - How do I add files from form to Firebase database? Vue js + Vuetify + Firebase 当我上传图像 firebase 存储 getdownloadUrl() 将下载 url 放入 Firestore Collections 但未在 React JS 中设置 - When I upload a image firebase storage getdownloadUrl() put download url In Firestore Collections but not set it in React JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM