我有一个包含2个对象的数据库.它看起来像这样:
users
- KEY
login: "xexe"
password: "123"
- KEY
login: "dede"
password: "123"
Run Code Online (Sandbox Code Playgroud)
现在我正在检查数据库中是否存在用户
constructor(private af: AngularFire) {
this.users = af.database.list('/users');
}
registerUser(login: string, password: string) {
this.af.database.list('/users', {
query: {
orderByChild: 'login',
equalTo: login
}
}).subscribe(response => {
if(response.length === 0) {
console.log("User does not exist");
//here i will add a user
} else {
console.log("Users exists");
}
})
}
Run Code Online (Sandbox Code Playgroud)
有什么问题?
让我们尝试使用"dede"登录注册用户(用户应该存在)
当我第一次单击提交按钮时,控制台显示:
Users exists- >嗯,这很好.
问题是当我第二次点击提交时(没有刷新网页)
然后console.log向我显示了两条消息
User does not exist
User exists
Run Code Online (Sandbox Code Playgroud)
这将为新用户添加不应该做的事情.为什么第二次订阅函数遍历每一行?怎么解决?
首先,抱歉长标题.
我想订阅连续流与数组的forEach从angularfire2,但我也想我已经证实后,第一组数据已经在运行一个函数:
this.people.forEach((person) => {
person.items = this.database.list('/items' + person.key);
person.items.subscribe((data) => {person.itemsList = data});
});
myIntendedFunction();
Run Code Online (Sandbox Code Playgroud)
有没有办法myIntendedFunction()这样:
data每个收到第一个流之后运行person,并且我想在启动时加载所有项目而不显示任何消息,但是一旦加载后.我想捕获订阅者中的任何新行并将其显示给桌面通知.
问题是,我不知道如何检查是否所有以前的项目都已加载,以及该行是新项目还是来自之前的现有项目.
this.items = this.af.database.list('notifications/'+this.uid+'/');
this.items.subscribe(list => {
list.forEach(row => {
// doing something here...
});
// once all the rows are finished loading, then any new row, show desktop notification message
});
Run Code Online (Sandbox Code Playgroud) observable rxjs firebase firebase-realtime-database angularfire2
我想知道是否可以在查询列表中使用"child_added"事件.像这样:
this.curUserPosts = this.af.database.list('/posts', {
query: {
orderByChild: 'user/id',
equalTo: id
}
}).$ref.on("child_added", (child) => {
console.log(child);
});
Run Code Online (Sandbox Code Playgroud)
我对以下内容感兴趣:它会起作用吗?仅当添加的子对应于查询时才会正常触发.性能如何.
而且,在firebase中处理这些查询列表的首选方法是什么?
rxjs firebase firebase-realtime-database angularfire2 angular
我遇到的空值字段的问题与我的数据库中不存在的键有关,所以这里的大部分话语都不适用于你的问题.如果您正在寻找一种在AngularFire2中"加入"查询的方法,下面接受的答案可以很好地解决这个问题.我正在使用combineLatest而不是forkJoin.为了做到这一点,你必须这样做import 'rxjs/add/observable/combineLatest';.
我有以下非规范化的Firebase结构:
members
-memberid1
-threads
-threadid1: true,
-threadid3: true
-username: "Adam"
...
threads
-threadid1
-author: memberid3
-quality: 67
-participants
-memberid2: true,
-memberid3: true
...
Run Code Online (Sandbox Code Playgroud)
我想username在我的threads视图中渲染,它按照排序quality.
我的服务:
getUsername(memberKey: string) {
return this.af.database.object('/members/' + memberKey + '/username')
}
getFeaturedThreads(): FirebaseListObservable<any[]> {
return this.af.database.list('/threads', {
query: {
orderByChild: 'quality',
endAt: 10
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的组件:
ngOnInit() {
this.threads = this.featuredThreadsService.getFeaturedThreads()
this.threads.subscribe(
allThreads =>
allThreads.forEach(thisThread => {
thisThread.username = this.featuredThreadsService.getUsername(thisThread.author) …Run Code Online (Sandbox Code Playgroud) firebase typescript firebase-realtime-database angularfire2 angular
我搜索了许多论坛,问题,在doc但找不到正确的解决方案.
使用angularfire2增加值的最佳方法是什么?
我看到我们可以使用[transaction()] []但实际上并不适用于angularfire2.还是快照?
user.service.ts
incrementLike(userToIncrementLike){
this.af.database.object('users/' + userToIncrementLike.uid).subscribe((userObject) => {
var newUser = {
likes: userObject.likes + 1
};
});
this.af.database.object('users/' + userToIncrementLike.uid).update(newUser);
}
Run Code Online (Sandbox Code Playgroud)
我也试过这种方式:
incrementLike(userToIncrementLike){
let obs = this.af.database.object('users/' + userToIncrementLike.uid);
obs.subscribe((snapshot) => {
let newValue = (snapshot.$value) ? (snapshot.$value + 1) : 1;
obs.set(newValue);
});
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你的帮助和提示:)路易斯.
我是Firebase的新手,并尝试使用AngularFire2使用Firebase + Firebase存储开发Ionic2应用.
我在存储上有很少的PDF和Firebase数据库中的URL.
我通过Firebase身份验证在应用上有经过身份验证的用户.
现在,当用户在他/她的手机上下载文件时,系统将获得downloadUrl,智能用户可以通过NeoLoad或任何其他工具查看它.然后,他可以与任何人共享该直接文件URL,并在没有该应用程序的情况下下载该pdf.
1-我想知道是否可以限制来自应用程序的文件访问权限,这样即使他必须提交URL,他也无法下载它.
2-是否可以生成在一段时间后过期的动态URL或仅用户特定的文件URL?
我有两个火灾商店集合与以下参考图像.
我想获得firstName和title.这里signup_id是从coll-signup文档id 引用的.以下代码给出了我的所作所为.
模型feed.ts
export interface Feed {
firstName? : string;
signup_id? : string;
title? : string;
}
Run Code Online (Sandbox Code Playgroud)
新闻feed.component模板
<ul *ngFor="let feed of feeds">
<!-- <li>{{feed.firstName}}</li> --> // Here I want to print my first name
<li>{{feed.title}}</li>
<li>{{feed.signup_id}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
新闻feed.component.ts
import { Component, OnInit } from '@angular/core';
import { Feed } from '../../models/feed';
import { FeedService } from '../../services/feed.service';
@Component({
selector: 'app-news-feed',
templateUrl: './news-feed.component.html',
styleUrls: ['./news-feed.component.css']
})
export class NewsFeedComponent implements OnInit …Run Code Online (Sandbox Code Playgroud) firebase typescript angularfire2 angular google-cloud-firestore
我正在开发一个Angular Firebase项目,我需要过滤我的数据库并获取关键值.目前我在我的服务代码中使用valueChanges()方法(在getUnreadBooks和getFavoriteBooks方法中,如下所示)来获取数据并对其进行过滤.但是当我尝试在模板文件中获取键值时,它给我的关键值为'undefined'.我尝试使用snapshotChanges()方法,但无法解决如何使用它来获取键值以及过滤数据.下面是我的Angular FirebaseService,home.component.ts(我在其中注入我的服务代码)和home.component.html(模板文件)代码:
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class FirebaseService {
books: Observable<any[]>;
unreadBooks;
favoriteBooks;
constructor(private db: AngularFireDatabase) {}
getBooks(){
this.books = this.db.list('/books').valueChanges() as Observable<any[]>;
return this.books;
}
getFavoriteBooks(){
this.favoriteBooks = this.db.list('/books').valueChanges() as Observable<any[]>;
this.favoriteBooks = this.favoriteBooks.map(books => {
const topRatedBooks = books.filter(item => item.rate>4);
return topRatedBooks;
})
return this.favoriteBooks;
}
getUnreadBooks(){
this.unreadBooks = this.db.list('/books').valueChanges() …Run Code Online (Sandbox Code Playgroud) 我无法使用data()DocumentSnapshot上的属性。它在控制台中给我一个错误。这是确切的错误:
auth.service.ts(72,20):错误TS2339:类型为“可观察”的属性“数据”不存在。
我尝试以多种不同方式获取数据。所有这些技术都是错误。
服务:
constructor(
private afAuth: AngularFireAuth,
private afs: AngularFirestore,
private router: Router,
public db: AngularFirestore
) {
this.user = afAuth.authState;
this.user.subscribe((user) => {
if (user) {
this.userDetails = user;
console.log(this.userDetails);
} else {
this.userDetails = null;
}
})
}
getUserName() {
(this.isLoggedIn()){
const userD = this.db.collection('users').doc(this.userDetails.uid);
const doc = userD.get();
return doc.data().firstName;
} else {
console.log('user not logged in');
return "nothing";
}
}
Run Code Online (Sandbox Code Playgroud)