rpaschoal / ng-chat

💬 A simple facebook/linkedin lookalike chat module for Angular applications.
MIT License
155 stars 92 forks source link

problems with getMessageHistoryByPage() #104

Closed TeDyGuN closed 5 years ago

TeDyGuN commented 5 years ago

hello, I have a question more of programming than ngChat, I'm stuck 3 days with this, that's why I see the need to open this issue. how can I get the messages from the database without an observable?, since I cant get the messages outside de http.get because it is a synchronous function.

public getMessageHistoryByPage(userId: any, size: number, page: number): Observable<Message[]> {
        let mockedHistory: Array<Message>;
        let url = 'http://localhost:3000/historial/' + userId + '/' + this.user.id;
        this.http.get(url)
            .pipe(map((result) => {
                    mockedHistory = result['messages'];
                    let startPosition: number = (page - 1) * size;
                    let endPosition: number = page * size;
                    mockedHistory = mockedHistory.slice(startPosition, endPosition);
                },
            )).subscribe();
        // In this line mockedHistory is empty and i can't return mockedHistory inside de get request.
        return of(mockedHistory.reverse()).pipe(delay(1000));
    }

Thanks for answer and sorry for the noob question.

rpaschoal commented 5 years ago

Hi @TeDyGuN ,

The adapter requires an Observable return. If your method to retrieve your data is synchronous you could just wrap your result with an Observable.of(yourDataHere) call.

Hope it helps! Cheers.

TeDyGuN commented 5 years ago

I solved with this code, thanks a lot!!

public getMessageHistoryByPage(userId: any, size: number, page: number): Observable<Message[]> {
        let url = 'http://localhost:3000/historial/' + userId + '/' + this.user.id + '/' + size + '/' + page;
        return this.http.get<Message[]>(url);
    }