2017-06-14 3 views
0

コンポーネント:配列が移入された後、関数を実行する方法が2角度

import { Component, OnInit } from '@angular/core'; 
import { Router, ActivatedRoute } from '@angular/router'; 

import { AF } from '../angularfire.service'; 

@Component({ 
    selector: 'app-add-friend', 
    templateUrl: './add-friend.component.html', 
    styleUrls: ['./add-friend.component.less'] 
}) 
export class AddFriendComponent implements OnInit { 
    profileInfo = []; 
    currentUser = []; 
    requestToggle: boolean = false; 
    showButtons: boolean = true; 

    constructor(private afService: AF, private route: ActivatedRoute, private router: Router) { 
    let id = this.route.snapshot.params['id']; 

    // Get viewed profile details. 
    this.afService.getAthleteProfile(id).subscribe((data) => { 
     if (data.length > 0) { 
     let details = data[0]; 
     this.profileInfo.push(details); 
     } 
    }); 

    // Get current logged in user details. 
    afService.getCurrentUserInfo().then(currentUserDetails => { 
     this.currentUser.push(currentUserDetails); 
    }); 
    } 

    // Send friend request. 
    addFriend() { 
    this.afService.addFriendRequest(this.profileInfo[0].userID, this.currentUser[0].userID, this.profileInfo[0].firstName, this.profileInfo[0].lastName, this.currentUser[0].firstName, this.currentUser[0].lastName).then(() => { 

    }); 
    } 

    ngOnInit() { 
    // Check if friend request has been sent to display appropriate button. 
    setTimeout (() => { 
    if (this.currentUser) { 
     this.afService.getFriendRequests(this.profileInfo[0].userID, this.currentUser[0].userID).subscribe((data) => { 
     if (data.length > 0) { 
      this.requestToggle = true; 
     } 
     }); 
     this.afService.getFriendRequestsFrom(this.profileInfo[0].userID, this.currentUser[0].userID) 
     .subscribe(data => { 
     if (data.length > 0) { 
      this.requestToggle = true; 
     } 
     }); 
    } 

    // Hides buttons if on own profile. 
    if (this.profileInfo[0].userID == this.currentUser[0].userID) { 
     this.showButtons = false; 
    } else { 
     this.showButtons = true; 
    } 
    }, 1000); 
    } 

} 

サービス:最後に向けて

import { Injectable } from "@angular/core"; 
import { AngularFireAuth } from 'angularfire2/auth'; 
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; 
import { Router, ActivatedRoute } from '@angular/router'; 
import { Observable } from 'rxjs/Rx'; 
import "rxjs/add/operator/map"; 

@Injectable() 
export class AF { 
    public currentUserDetails; 

    constructor(public af: AngularFireAuth, private database: AngularFireDatabase, private router: Router) { 
    } 

// Get all currently logged in user details. 
getCurrentUserInfo() { 
    return new Promise((resolve, reject) => { 
    this.af.authState.subscribe(authData => { 
     let email = authData.email; 
     let array = this.database.list('registeredUsers', { 
     query: { 
     orderByChild: 'email', 
     equalTo: email 
     } 
    }).subscribe(data => { 
     let currentUserDetails = data[0]; 
     resolve(currentUserDetails); 
     }); 
    }); 
    }); 
} 

// Get all users details. 
getAllAthletes(): Observable<any> { 
    return this.database.list('registeredUsers'); 
} 

// Get useer details for the current profile route. 
getAthleteProfile(userID) { 
    return this.database.list('registeredUsers', { 
    query: { 
     orderByKey: userID, 
     equalTo: userID 
    } 
    }) 
} 

// Register new user. 
registerUser(email, password) { 
    return this.af.auth.createUserWithEmailAndPassword(email, password); 
} 

// Add new user details to database. 
addUserEntry(uid, fname, lname, email) { 
    return this.database.object('registeredUsers/' + uid).set({ 
    firstName: fname, 
    lastName: lname, 
    email: email, 
    userID: uid, 
    bio: "Your short bio goes here...", 
    phone: 5555555555 
    }); 
} 

// Add friend request to both parties. 
addFriendRequest(profileUID, currentUID, profileFirstName, profileLastName, currentFirstName, currentLastName) { 
    return this.database.object('registeredUsers/' + currentUID + '/friends/requests/requestTo/' + profileUID).set({ 
    requestTo: profileUID, 
    firstName: profileFirstName, 
    lastName: profileLastName 
    }).then(() => 
    this.database.object('registeredUsers/' + profileUID + '/friends/requests/requestFrom/' + currentUID).set({ 
    requestFrom: currentUID, 
    firstName: currentFirstName, 
    lastName: currentLastName 
    }) 
)} 

/* (TODO): Consolidate lines 81-99. Replace the paths with variables set in the components. 
---* 
---*/ 

// Check if user sent a friend request to user being viewed. 
getFriendRequests(profileUID, currentUID) { 
    return this.database.list('registeredUsers/' + currentUID + '/friends/requests/requestTo/' + profileUID) 
} 

// Check if user received a friend request from user being viewed. 
getFriendRequestsFrom(profileUID, currentUID) { 
    return this.database.list('registeredUsers/' + currentUID + '/friends/requests/requestFrom/' + profileUID) 
} 

// Get all sent friend requests. 
getPendingFriendRequests(currentUID) { 
    return this.database.list('registeredUsers/' + currentUID + '/friends/requests/requestTo/'); 
} 

// Get all received friend requests. 
getPendingFriendRequestsFrom(currentUID) { 
    return this.database.list('registeredUsers/' + currentUID + '/friends/requests/requestFrom/'); 
} 

// Update the user account info. 
updateUserEntry(uid, fname, lname, phone, bio) { 
    return this.database.object('registeredUsers/' + uid).update({ 
    firstName: fname, 
    lastName: lname, 
    phone: phone, 
    bio: bio 
    }); 
} 

// Login 
loginWithEmail(email, password) { 
    return this.af.auth.signInWithEmailAndPassword(email, password).then(()=> { 
    this.router.navigate(['/dashboard']); 
    }).catch(function(error) { 
    var errorMessage = error.message; 
    }) 
} 

// Logout 
logout() { 
    this.af.auth.signOut().then(() => 
    this.router.navigate(['/login']) 
)} 
} 

、私は配列ので1秒setTimeout機能に包まれた機能を持っています正しく実行するには、currentUserを入力する必要があります。配列には常に値が設定されますが、十分に高速ではないので、関数はsetTimeoutがなくても実行されません。

setTimeoutを使用する代わりに、角度をにする方法はありますか?「このアレイを実装した後にこの機能を実行する」?すべての

+1

を 'その後、()'内の機能を実行していないのはなぜこれは、両方の観測がcompleatedする必要があること、を意味しますか? – Dinistro

+0

_配列には常に値が設定されます_ - 非同期に値が設定されますか? –

+0

@Dinistro同じ問題。この関数は、.then()の内部にある.pushの後に実行する必要があります。 –

答えて

2

まず、サービスでPromiseは必要ありません、あなたは、単に、観察を返すことができます。その後

import 'rxjs/add/operator/mergeMap'; 



getCurrentUserInfo() { 
    return this.af.authState.mergeMap(authData => { 
     return this.database.list('registeredUsers', { 
      query: { 
       orderByChild: 'email', 
       equalTo: authData.email 
      } 
     }); 
    }).map(data => { 
     let currentUserDetails = data[0]; 
     resolve(currentUserDetails); 
    }); 
} 

Note: If you don't know how observables work, you should take a look at some tutorials.

を、あなたのコンポーネントでこれを使用することができます。われわれが知っているとおり、私たちはあなたの機能を実行できるように、currentUserprofileInfoが必要です。

import 'rxjs/add/observable/forkJoin'; 

Observable.forkJoin(
    this.afService.getAthleteProfile(id), 
    this.afService.getCurrentUserInfo() 
).subscribe((results) => { 
    results[0]; //This is the result of getAthleteProfile 
    results[1]; //This is the result of getCurrentUserInfo 

    //Now, push the values into the arrays 
    yourFunction(); //and execute your function here 
}); 

注:あなたの観察可能な演算子をインポートすることを忘れないでください:

import 'rxjs/add/observable/forkJoin'; 
import 'rxjs/add/operator/mergeMap'; 
関連する問題