标签: angularfire2

如何使用AngularFire2创建用户登录电子邮件和密码

我创建了一个login-service用谷歌,脸书或推特登录的选项,它运作良好.

我正在尝试使用电子邮件和密码创建登录选项(该选项在firebase控制台中启用),我无法使其正常工作.

我的登录服务代码如下:

import {Injectable, OnInit} from '@angular/core';
import {AngularFire, AuthProviders, AuthMethods} from "angularfire2/angularfire2";


@Injectable()
export class LoginService implements OnInit {
public isLoged: boolean;

ngOnInit() {

}

constructor(private af: AngularFire) {
  this.af.auth.subscribe(user => {
    if (user) {
      this.isLoged = true;
      console.info("Se ha logueado correctamente")
    } else {
      this.isLoged = false;
      console.info("Se ha deslogueado correctamente");
    }
  });
}

login() {
  this.af.auth.login();
}

loginGoogle() {
  //this is the default login , that it's setup in the main.js
  this.login();
}

loginFacebook() …
Run Code Online (Sandbox Code Playgroud)

firebase firebase-authentication angularfire2 angular

4
推荐指数
1
解决办法
8374
查看次数

Firebase数据与Observable连接

目前,我遇到了Firebase Observable加入的问题.

我真的不知道哪种方法可以从不同的对象中获取数据并将它们连接在一起.

我的数据结构:

users {
    userid1 {
        conversationid: id
        ...
    },
    userid2 {
       ...
    }
}

conversations {
    conversationid {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想得到当前用户的所有对话.要获取当前用户ID,我将订阅auth Observable,如下所示:

 this.af.auth.subscribe(auth => {
    console.log(auth.uid);
 });
Run Code Online (Sandbox Code Playgroud)

接下来我需要用户的子对象来获取会话ID.我是这样做的:

 //needs the userid from Observable on top 
 this.af.database.object('/users/' + auth.uid)
     .map(
         user => {
             console.log(user.conversationid);
         }
      )
      .subscribe();
Run Code Online (Sandbox Code Playgroud)

对话也一样:

//needs the conversationid from the second Observable 
this.af.database.list('/conversations/' + user.conversationid)
    .subscribe();
Run Code Online (Sandbox Code Playgroud)

如您所见,有3个Observable.我知道可以嵌套它们,但在我的项目中,这可能会发生多达5次.

是否可以在不嵌套3个Observable的情况下进行对话?

observable rxjs firebase firebase-realtime-database angularfire2

4
推荐指数
1
解决办法
1910
查看次数

使用Angular2/AngularFire2创建或增加值

我正在使用Angular 2和AngularFire 2与Firebase进行交互.在Firebase中,我有一个标签集合.我想创建或增加标签的数量.我正在使用的代码看起来像这样:

let tagName = "angular";
let tagObs = this.af.database.object(`/tags/${tagName}`);
tagObs.subscribe(function(snapshot) {
    let newValue = (snapshot.$value) ? (snapshot.$value + 1) : 1;
    this.tagObs.set(newValue);
}.bind({ tagObs: tagObs ));
Run Code Online (Sandbox Code Playgroud)

我不清楚为什么,但这不起作用.它创建了一个无限循环,只是不断增加标记值.

使用AngularFire 2,我应该如何创建或增加节点的值(在这种情况下为"标签")?

@Fiddle评论后更新

这是具有"胖箭头"功能的相同代码.存在同样的问题......无限循环.

let tagName = "angular";
let tagObs = this.af.database.object(`/tags/${tagName}`);
tagObs.subscribe((snapshot) => {
    let newValue = (snapshot.$value) ? (snapshot.$value + 1) : 1;
    tagObs.set(newValue);
});
Run Code Online (Sandbox Code Playgroud)

更新#2:有效的代码

为了清楚起见,这是我最终使用的实际代码:

let tagObs = this.af.database.object(`/tags/${tagName}`);
tagObs.transaction(function(currentCount) {
  return currentCount + 1;
});
Run Code Online (Sandbox Code Playgroud)

angularfire2 angular

4
推荐指数
1
解决办法
1624
查看次数

AngularFire2取消订阅未注册订阅时的注销,数据库权限错误

我在这里使用AngularFire2版本的代码,我用它来从我的一个组件上的数据库中获取一些成员.

当我在同一个组件中调用此注销时,我遇到了问题.我取消订阅订阅但我仍然在控制台中收到错误说Exception was thrown by user callback. Error: permission_denied at /users/uid.我知道这是因为我不再登录,我的数据库规则不允许这种读取操作.如果我已经取消订阅,我不知道为什么它还在尝试读取数据.

    constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
        this.user = afAuth.authState;

        this.userSub = this.user.subscribe(auth => {
            if (auth) {
                this.member = db.object('/users/' + auth.uid);
                this.dbSub = this.member.subscribe();
            }
        });
    }

    logout() {
        this.dbSub.unsubscribe();
        this.userSub.unsubscribe();

        this.afAuth.auth.signOut()
            .then(() => this.router.navigateByUrl('/login'));
    }
Run Code Online (Sandbox Code Playgroud)

observable firebase firebase-realtime-database angularfire2 angular

4
推荐指数
1
解决办法
1361
查看次数

'QueryFn'|类型中不存在'query' angularfire2

类型'{query:{limitTolast:number; orderByKey:boolean; }; ''不能赋值给'QueryFn'类型的参数.对象文字只能指定已知属性,'QueryFn'类型中不存在'query'.

的package.json

"angularfire2": "^5.0.0-rc.3",
"firebase": "^4.5.1",
Run Code Online (Sandbox Code Playgroud)

chat.service.ts

getMessages(): FirebaseListObservable<ChatMessage[]> {
    return this.db.list('messages', {
      query: { limitTolast : 25, orderByKey: true}
    });
  }
Run Code Online (Sandbox Code Playgroud)

firebase typescript angular-cli angularfire2

4
推荐指数
1
解决办法
7517
查看次数

如何在Angular 5中的Firestore集合中包含文档ID

我有这个功能,获取所有帖子,我想得到帖子的ID任何想法如何获取帖子的ID或如何在我将文档添加到集合时包含id

get_the_posts(){
    this.Post_collection = this.afs.collection('posts'); 
    return this.Posts = this.Post_collection.valueChanges();
}
Run Code Online (Sandbox Code Playgroud)

这给了我这个输出:

[
   { name: 'post 1 name' description: 'post description'}, 
   { name: 'post 2 name' description: 'post description'},
   { name: 'post 3 name' description: 'post description'},
]
Run Code Online (Sandbox Code Playgroud)

我想要这个输出

[
   { id: 'm9mggfJtClLCvqeQ', name: 'post 1 name' description: 'post description'}, 
   { id: 'm9mggfJtCldsadaf', name: 'post 2 name' description: 'post description'},
   { id: 'm9mggfJtCdsasavq', name: 'post 3 name' description: 'post description'},
]
Run Code Online (Sandbox Code Playgroud)

angularfire2 google-cloud-firestore

4
推荐指数
2
解决办法
2762
查看次数

如何使用“ get”方法检索angularfire2中的所有集合

Firebase云存储提供了“获取”方法来检索整个集合,如下所示-

db.collection("users").get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(doc);
    });
});
Run Code Online (Sandbox Code Playgroud)

我在Ionic 3项目中使用Angularfire2版本5.0.0rc3与Firebase云存储连接。

我试图按以下方式访问此get方法-

constructor(
    private afs: AngularFirestore
) {

    this.afs.collection("users").get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            console.log(doc);
        });
    });

}
Run Code Online (Sandbox Code Playgroud)

但是在这里,“获取”方法不起作用。谁能告诉我谁将这种“ get”方法与firebase clould存储和angularfire2一起使用。

firebase angularfire2 ionic3 angular google-cloud-firestore

4
推荐指数
2
解决办法
5431
查看次数

forkJoin不适用于AngularFire2 valueChanges

请帮我解决一个我正在努力的问题.

我有一组Firebase对象键

const keys = ['-Kx9pqoMWlJLbKLQcAkP', '-Kx9pqoOYlDHTJ64Was5']
Run Code Online (Sandbox Code Playgroud)

我要做的是使用一个流中的所有Firebase对象forkJoin.这就是我所拥有的:

const obj1 = this.fbService.getObj(keys[0]);
const obj2 = this.fbService.getObj(keys[1]);

forkJoin([obj1, obj2])
    .subscribe(res => {
        console.log(res);  // <-- this never happens
    };
Run Code Online (Sandbox Code Playgroud)

fbService方法是:

getObj(key): Observable<MyObj> {
  return this.fb.object(`/path/to/obj/${key}`).valueChanges();
}
Run Code Online (Sandbox Code Playgroud)

我认为这种getObj方法效果不好forkJoin,也许是因为valueChanges我正确使用它?

然而:

那么,我做错了什么?我的目标是从键数组中获取对象数组:

['-Kx9pqoMWlJLbKLQcAkP', '-Kx9pqoOYlDHTJ64Was5'] => [{prop:'val'},{prop:'val2'}]
Run Code Online (Sandbox Code Playgroud)

rxjs firebase rxjs5 angularfire2 angular

4
推荐指数
1
解决办法
482
查看次数

Firestore-如何从值映射中添加/减去

我正在按照Firestore的说明存储阵列:https//firebase.google.com/docs/firestore/solutions/arrays

现在我想做的就是推到这张地图。例如现在我有:

Contacts
   contact1: true
Run Code Online (Sandbox Code Playgroud)

但是我想添加或删除一个联系人,例如:

Contacts
   contact1: true
   contact2: true
Run Code Online (Sandbox Code Playgroud)

我尝试获取Contacts映射并使用push方法,但是由于它不是传统数组,因此我认为这不会起作用。例如:

this.afs
  .doc(`groups/${group.id}`)
  .ref.get()
  .then(doc => {
    let contacts: Array<any> = doc.data().contacts;

    contacts.push({ // error here as push is not a function
      [contactId]: true
    });

    console.log(contacts);
  });
Run Code Online (Sandbox Code Playgroud)

有没有更简单的方法可以执行此操作-我错过了什么吗?

firebase angularfire2 angular google-cloud-firestore

4
推荐指数
2
解决办法
4382
查看次数

Firebase 5 Angular 5 AngularFireList.snapshotChanges()错误

当我尝试在Angular 5/Firebase5应用程序中订阅AngularFireList时出现以下错误.

zone.js:192 Uncaught TypeError: Object(...) is not a function
    at SwitchMapSubscriber.eval [as project] (changes.js:7)
    at SwitchMapSubscriber._next (switchMap.js:91)
    at SwitchMapSubscriber.Subscriber.next (Subscriber.js:95)
    at RefCountSubscriber.Subscriber._next (Subscriber.js:131)
    at RefCountSubscriber.Subscriber.next (Subscriber.js:95)
    at Subject.next (Subject.js:56)
    at ConnectableSubscriber.Subscriber._next (Subscriber.js:131)
    at ConnectableSubscriber.Subscriber.next (Subscriber.js:95)
    at Notification.observe (Notification.js:32)
    at AsyncAction.DelaySubscriber.dispatch (delay.js:91)
Run Code Online (Sandbox Code Playgroud)

我的服务和控制器类内容如下,

1)名为'FirebaseService'的服务

customers: AngularFireList<any>;
getCustomers(){
    this.customers = this.fire.list('users');
    return this.customers;
  }
Run Code Online (Sandbox Code Playgroud)

2)控制器

constructor(private firebase: FirebaseService) { }

serviceProviders: ServiceProvider[];
var x = this.firebase.getServiceProviders();
    x.snapshotChanges().subscribe(item => {
      this.serviceProviders = [];      
      item.forEach(element => {
        var y = element.payload.toJSON();
        y["$key"] = …
Run Code Online (Sandbox Code Playgroud)

firebase firebase-realtime-database angularfire2 angular5

4
推荐指数
1
解决办法
2281
查看次数