使用angularfire2

时间:2018-01-04 15:10:55

标签: firebase firebase-authentication ionic3 angularfire2 angular5

我正在开发一个简单的应用,我需要从特定用户获取数据。 Heres是我在服务/提供商中的代码

constructor(private afs: AngularFirestore, private afAuth: AngularFireAuth){
    this.afAuth.authState.subscribe(
        user => {
            this.notesCollection = this.afs.collection<Note>('notes', ref =>
                ref.orderBy('created', "desc")
                .where('trashed', '==', false)
                .where('user', '==', user.email));
        }
    );
}
fetchNotes(): Observable<NoteId[]> {
    this.notes = this.notesCollection.snapshotChanges().map(actions =>{
        return actions.map(a => {
            const data = a.payload.doc.data() as Note;
            const id = a.payload.doc.id;
            return {id, ...data};
        })
    });
    return this.notes;
}

但是,当我从我的页面调用fetchNotes方法时,我得到一个错误,基本上说notes变量是未定义的。我知道这是由于用户在构造函数中可观察执行,但是,我不知道任何其他方式来获取用户信息和进行查询,因此,如果您可以帮助找到另一种方法来执行此操作,我会真的很感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我会这样工作:

@foreach (var d in data)
{
    if (Condition1)
    {
        <div class="form-group">

            <label class="col-sm-3 control-label">
                Text 1
            </label>
            <div class="col-sm-8">

                @Html.TextBox("Box1")

            </div>

        </div>
    }
    else if(Condition 2)
    {
        <div class="form-group">
            <label class="col-sm-3 control-label">
                Text 2
            </label>

            <div class="col-sm-8">

                @Html.TextBox("Box2")

            </div>
        </div>
    }

     else if(Condition3)
     {
        <form class="form-inline">
            <label class="required col-sm-3 control-label">
                @*Horsepower and RPM*@
            </label>
            <div class="col-sm-2">
                @{
                    @Html.TextBox("Box2")
                }
            </div>
        </form>       
     }
}

所以关键概念是A)拥抱Observables(notes: Observable<NoteId[]>; constructor(private afs: AngularFirestore, private afAuth: AngularFireAuth){ this.notes = this.afAuth.authState.switchMap(user => { if (user) { return this.afs.collection<Note>('notes', ref => ref.orderBy('created', "desc") .where('trashed', '==', false) .where('user', '==', user.email)).snapshotChanges() } else { return of([]); } }).map(actions =>{ return actions.map(a => { const data = a.payload.doc.data() as Note; const id = a.payload.doc.id; return {id, ...data}; }) }); } 可能会让你遇到麻烦,如果因某种原因需要当前值使用BehaviorSubject)B)switchMap authState C)map on assigns < / p>