简体   繁体   English

如何在创建用户时将其添加到“用户”Firestore 中?

[英]How do I make it so when I create a user it gets added to a 'user' Firestore?

Currently having difficulty figuring out how to add the ability that, when an account is created, the account is added to the Firestore under a collection called 'users'.目前很难弄清楚如何添加这样的功能,即在创建帐户时,该帐户会在名为“用户”的集合下添加到 Firestore。 From what I have seen from other people they added in something like this根据我从其他人那里看到的,他们添加了这样的内容

.then then(userCredential => {
firestore.collection('users').doc(userCredential.user.uid).set({name})
}

This is my code as it stands after my attempt at it.这是我尝试后的代码。 I am unsure where I should be adding in my code.我不确定我应该在我的代码中添加的位置。 Currently I have it under this.fireauth.auth.createUserWithEmailAndPassword(this.email, this.password) and the attempt I did is there the program works fine with how it is, I can register an account and see the account in the authentication on the firebase authentication users tab but no 'user' collection is created in the Firestore.目前我在this.fireauth.auth.createUserWithEmailAndPassword(this.email, this.password)下拥有它,我所做的尝试是该程序可以正常工作,我可以注册一个帐户并在身份验证中查看该帐户firebase 身份验证用户选项卡,但在 Firestore 中未创建“用户”集合。 I am currently stumped where I should go from here.我目前很难从这里开始 go 。 All I really need is the User-Id/name to be stored in the Firestore.我真正需要的是存储在 Firestore 中的用户 ID/名称。

import { Component, OnInit } from '@angular/core';
import { Platform, AlertController } from '@ionic/angular';
import { LoadingController, ToastController } from '@ionic/angular';
import { Router } from '@angular/router';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';

@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage {
  email: string = '';
  password: string = '';
  error: string = '';
  username: string = '';
  image: number;
  constructor(private fireauth: AngularFireAuth, private router: Router, private toastController: ToastController, private platform: Platform, public loadingController: LoadingController,
    public alertController: AlertController, private firestore: AngularFirestore) {

  }

  async openLoader() {
    const loading = await this.loadingController.create({
      message: 'Please Wait ...',
      duration: 2000
    });
    await loading.present();
  }
  async closeLoading() {
    return await this.loadingController.dismiss();
  }

  signup() {
    this.fireauth.auth.createUserWithEmailAndPassword(this.email, this.password)
      .then(res => {
        if (res.user) {
          console.log(res.user);
          this.updateProfile();
          userCredential => this.firestore.collection('users').doc(userCredential.user.uid).set({
              name
            })
        }
      })
      .catch(err => {
        console.log(`login failed ${err}`);
        this.error = err.message;
      });
  }

  updateProfile() {
    this.fireauth.auth.onAuthStateChanged((user) => {
      if (user) {
        console.log(user);
        user.updateProfile({
          displayName: this.username,
          photoURL: `https://picsum.photos/id/${this.image}/200/200`
        })
          .then(() => {
            this.router.navigateByUrl('/home');
          })
      }
    })
  }

  async presentToast(message, show_button, position, duration) {
    const toast = await this.toastController.create({
      message: message,
      showCloseButton: show_button,
      position: position,
      duration: duration
    });
    toast.present();
  }
}

This is the current rules that I have set for my database.这是我为我的数据库设置的当前规则。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {


    match /users/{userId} {
    allow read: if request.auth != null;
    allow write: if request.auth.uid == userId;
}
  }
}

It looks like there is a problem with the arrow function:看起来箭头function有问题:

userCredential => this.firestore.collection('users').doc(userCredential.user.uid).set({name})

It seems you only initialize function but never call it.看来您只初始化 function 但从不调用它。 Try change this line to just:尝试将此行更改为:

this.firestore.collection('users').doc(user.uid).set({name})

or call the anonymous function if you prefer your solution for example:或致电匿名 function 如果您更喜欢您的解决方案,例如:

//... some logic
const initialize = user => this.firestore.collection('users').doc(user.uid).set({name})

initialize(user)

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

相关问题 Firebase/Firestore - 如何为新用户创建新文档 - Firebase/Firestore - How do I create a new document for a new user 创建新用户时如何将数据写入firestore? - How do I write data to firestore when creating a new user? 当用户标记 discord 机器人时,我该如何做到这一点,它会回复一条消息? - How do I make it so when a user tags the discord bot, it responds with a message? 我怎样才能做到这一点,当用户输入一个字母时,输入会更改为下一个输入字段等等? - How can I make it so that when a user types a letter the input would change to the next input field and so on? 我怎样才能使文本只添加一次? - How do I make it so the text is only added once? 在Firebase中使用用户副本时,$ conf会添加到对象中,因此我无法将用户复制到新位置吗? - When working with a copy of a user in firebase $$conf is added to the object so I can't copy the user to a new location? 如何让用户每分钟只获得一次 xp,而不是每条消息一次? - How do I make it such that the user gets xp only once per minute, instead of once per message? 用户离开网站时如何重置计时器 - How do i make timer reset when user leave website Google Maps API:如何使用户添加的标记成为永久标记? - Google Maps API: how do I make user-added markers permanent? 如何使每个用户都有自己的状态? - How do I make it so each user has its own state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM