[英]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.