假设我正在收集用户名,并且我想检查此用户名是否已存在于我的Firebase数据库中.AngularFire2 update或set方法实际上并不检查对象是否存在,但如果是,则替换它.有没有办法检查并说出返回一个可观察到的错误?
目前我的解决方案是检索对象,如果有结果,我知道它存在.我正在寻找一种更简单的方法来检查它.
我需要将其作为数据库对象,而不是身份验证中的实际用户.
let userExists = false;
this.af.database.object('/users/' + newUser.name).subscribe( user => {
if (user) {
userExists = true;
console.log('This username is taken. Try another one');
}
else {
this._af.database.object('/users/' + newUser.name).update({
email: 'johndoe@example.com',
password: '!@#$1234'
})
}
});
Run Code Online (Sandbox Code Playgroud) 我可以轻松地使用它来创建用户:
this.af.auth.createUser({email: email, password: password});
Run Code Online (Sandbox Code Playgroud)
但是一旦创建用户详细信息,我该如何编辑它们?(即:更改密码或电子邮件地址?)。我会认为是这样的:
this.af.auth.updateUser({email: email, password: password});
Run Code Online (Sandbox Code Playgroud)
但是没有updateUser方法吗?
我有问题
import { AngularFireDatabase, FirebaseListObservable } from "angularfire2/database";
Run Code Online (Sandbox Code Playgroud)
我导入了AngularFireDatabase但FirebaseListObservable在搜索此帖子后出现了红线帮我解决了我的问题 在AngularFire2中出现错误:"没有导出的成员AngularFire,AuthProviders,AUthMethods,FirebaseListObservable"?
import { AngularFireDatabase, FirebaseListObservable } from "angularfire2/database-deprecated";
Run Code Online (Sandbox Code Playgroud)
但是当我编译我得到它并且它无法帮助我不知道该怎么做搜索ect core.es5.js:1020错误错误:未捕获(在承诺中):错误:没有AngularFireDatabase的提供者!错误:没有AngularFireDatabase的提供程序!
"angularfire2":"^ 5.0.0-rc.2","core-js":"^ 2.4.1","firebase":"^ 4.5.0",
firebase typescript firebase-realtime-database angularfire2 angular
我正在构建一个应用程序,其中用户可以匿名登录,稍后可以选择使用 Google 或 facebook 登录。我已按照firebase 网站和这篇文章中提到的步骤进行操作,但仍然收到错误消息 -
代码:“auth/provider-already-linked”,
消息:“用户只能链接到给定提供商的一个身份。”
以下是我的auth.service.ts内容
@Injectable()
export class AuthService {
user: Observable<User>;
constructor(private afAuth: AngularFireAuth,
private afs: AngularFirestore,
private router: Router) {
this.user = this.afAuth.authState
.switchMap(user => {
if (user) {
return this.afs.doc<User>(`usersData/${user.uid}`).valueChanges()
} else {
return Observable.of(null)
}
});
}
anonymousLogin(): Promise<any>{
return this.afAuth.auth.signInAnonymously();
}
get authState(): Observable<User>{
return this.afAuth.authState.map(data => {
return data;
});
}
get currentUser(): boolean {
return this.user !== null; …Run Code Online (Sandbox Code Playgroud) firebase firebase-authentication angularfire2 google-cloud-firestore
有没有办法保留同一集合的引用,但使用 firestore 更改排序?
TLDR:这就像我想要实现的功能: https ://stackoverflow.com/a/17320018,但是由于我的数据来自 firestore,我还没有完全找到动态完成此操作的正确方法。
假设我有一个messagesService.ts包含消息集合和对消息 Observable 的引用:
messagesCollection: AngularFirestoreCollection<Message>
messages: Observable<Message[]>;
this.messagesCollection = this.db.collection('messages', ref => {
return ref.orderBy('dateCreated', 'desc');
});
this.messages= this.messagesCollection.valueChanges();
Run Code Online (Sandbox Code Playgroud)
当我将此数据弹出时,*ngFor="let message of messages | async"它会按预期在顶部显示最新消息。
我需要帮助的地方:
我希望能够单击一个按钮并更改消息的顺序(或我从 firestore 获得的数据的顺序)。例如,您可以单击一个按钮,按最喜欢、最高浏览量或最旧消息、按字母顺序排列的消息进行排序。我是否需要为要对同一集合进行排序的每一种方式都有单独的参考?这看起来很草率,有没有更干净的方法来动态地做到这一点?
最初,我尝试了这样的事情:
sortMessagesBy(field) {
this.messagesCollection = this.db.collection('messages', ref => {
return ref.orderBy(field, 'desc');
});
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为它似乎更改了对集合的引用,因此未更新可观察的消息。
所以接下来,我尝试了这个:
sortMessagesBy(field) {
this.messagesCollection = this.db.collection('messages', ref => {
return ref.orderBy(field, 'desc');
});
this.messages= this.messagesCollection.valueChanges();
return this.messages;
}
Run Code Online (Sandbox Code Playgroud)
但这似乎创建了一个新的 this.messages 对象,它使 ngFor 重新渲染整个页面并且看起来非常草率(即使使用 …
javascript firebase angularfire2 angular google-cloud-firestore
我在 Firestore 中创建了一个集合“Person”。我能够从代码创建文档和字段。
ngOnInit() {
this.personCol = this.afs.collection('person');
//snapshotChanges is used to retreive the document data and other metadata, which includes the ID
//we need ID to edit or delete a record
this.person = this.personCol.snapshotChanges()
.map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Person;
const id = a.payload.doc.id;
return { id, data };
});
});}
addPerson() {
this.afs.collection('person').add({
'name': this.name,
'age': this.age,
});}
getPerson(PersonId) {
this.personDoc = this.afs.doc('person/'+personId);
this.singlePerson = this.personDoc.valueChanges();
Run Code Online (Sandbox Code Playgroud)
}
现在我想添加该人借阅的书籍列表:
Person:
{Name: "John" …Run Code Online (Sandbox Code Playgroud) 我像这样使用 Firestore。
用例 1:
contacts$: Observable<ContactDetail[]>;
constructor(){
}
ionViewDidEnter() {
this.contacts$ = this.contactProvider.getSpecificUserContacts(this.authenticationProvider.user.uid).valueChanges();
this.contacts$.pipe(first()).subscribe(res => { },
err => { },
() => { }
);
}
Run Code Online (Sandbox Code Playgroud)
用例 2:
getAllContactCategories() {
this.categories$ = this.categoryProvider.getAllContactCategories().valueChanges();
this.categories$.subscribe(res => {
this.categorySortedList = res;
},
err => { }
);
}
Run Code Online (Sandbox Code Playgroud)
但我从来没有unsubscribed。那我需要这样做吗?否则,会不会导致内存泄漏并耗尽电池使用量?我知道我们不需要unsubscribed角度HTTP服务,因为它是由框架本身自动完成的。那么 Firestore/Angularfire2observables呢?我从未在firestore书籍或文章或类似的东西中看到过这样的模式。
应用程序组件:
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from 'angularfire2/firestore';
import { RoomComponent } from './component/room/room.component';
import { AngularFireAuth } from '@angular/fire/auth';
import { FirebaseService } from "./service/firebase.service";
import { AngularFireAuthModule } from 'angularfire2/auth';
imports: [
BrowserModule,
AngularFireModule.initializeApp(firebaseconfig),
AngularFirestoreModule
],
providers: [
FirebaseService,
{
provide: PERFECT_SCROLLBAR_CONFIG,
useValue: DEFAULT_PERFECT_SCROLLBAR_CONFIG
},
AngularFireAuth
],
Run Code Online (Sandbox Code Playgroud)
package.json 文件:
{
"dependencies": {
"@angular/animations": "^5.2.11",
"@angular/cdk": "^5.2.4",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/material": "^5.2.4",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": …Run Code Online (Sandbox Code Playgroud) 请参阅此处的问题(打开控制台):
https://stackblitz.com/edit/angular-su5p89?file=src%2Fapp%2Fapp.module.ts
我已经像这样导入并调用initalizeApp了我的AppModule:
import { AngularFireModule } from '@angular/fire';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { AngularFirestoreModule } from '@angular/fire/firestore';
@NgModule({
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
// Firebase
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule,
AngularFireAuthModule,
// App
AppRoutingModule
],
declarations: [
AppComponent
],
bootstrap: [AppComponent]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个看起来像这样的守卫:
import { Injectable } from '@angular/core';
import { CanActivate, CanActivateChild, CanLoad } from '@angular/router';
import * as firebase from 'firebase/app';
@Injectable({
providedIn: 'root'
})
export class AuthGuard …Run Code Online (Sandbox Code Playgroud) 我正在为一个大学项目(一个 ToDoList 移动应用程序)学习 Ionic4 和 Angular。我正在尝试使用 AngularFirestore.collection[...].valueChanges() 从我的 firestore 数据库中获取一些数据,并将其存储在我的自定义服务的成员变量中,以便之后将其提供给组件。但是我在 Visual Studio Code 和 ionic 控制台中都遇到了 TS2322 错误。
错误如下:
Type 'Observable<ToDoList[]>' is missing the following properties from type Observable<ToDoList[]>': buffer, bufferCount, bufferTime, bufferToggle, and 104 more.'
我遵循了这两个教程: - https://angularfirebase.com/lessons/firestore-advanced-usage-angularfire/ - https://www.techiediaries.com/ionic-firestore-crud/ 我试图移动所有组件的相关代码,更改我的 AngularFirestore.collection 行,灵感来自https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md 我也尝试删除 AngularFireAuth 的导入,以及行 this.afAuth.auth.signInAnonymously();
我的列表服务:
import { Injectable } from '@angular/core';
import {ToDoItem, ToDoList} from '../models/todo-model';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { AngularFirestoreCollection, AngularFirestore } …Run Code Online (Sandbox Code Playgroud)