簡體   English   中英

如何在創建用戶時將其添加到“用戶”Firestore 中?

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

目前很難弄清楚如何添加這樣的功能,即在創建帳戶時,該帳戶會在名為“用戶”的集合下添加到 Firestore。 根據我從其他人那里看到的,他們添加了這樣的內容

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

這是我嘗試后的代碼。 我不確定我應該在我的代碼中添加的位置。 目前我在this.fireauth.auth.createUserWithEmailAndPassword(this.email, this.password)下擁有它,我所做的嘗試是該程序可以正常工作,我可以注冊一個帳戶並在身份驗證中查看該帳戶firebase 身份驗證用戶選項卡,但在 Firestore 中未創建“用戶”集合。 我目前很難從這里開始 go 。 我真正需要的是存儲在 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();
  }
}

這是我為我的數據庫設置的當前規則。

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;
}
  }
}

看起來箭頭function有問題:

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

看來您只初始化 function 但從不調用它。 嘗試將此行更改為:

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

或致電匿名 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM