Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: return Array as Observable

How can I return an array as Observable in getTodoItems()?

It was Promise<Itodo[]> and I used Promise.Resolve(TodoItems) to return the array. But when I changed it to use Observable instead of Promise, I don't know how to return the data and couldn't make it work.

I tried Observable.of(TodoItems) but it doesn't know the .of function.

I keep getting these errors whatever I tried. Could you please help me with it? Thanks.

enter image description here

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/fromArray';

import { Itodo } from './todo'

const TodoItems: Itodo[] = [
    { todoId: 11, description: 'Mr. Nice' },
    { todoId: 12, description: 'Narco' },
    { todoId: 13, description: 'Bombasto' }
]

@Injectable()
export class TodoService {


    constructor(private _http: Http) {
    }

    getTodoItems(): Observable<Itodo[]> {

        return TodoItems.map((items => Itodo[]) => <Itodo[]>items);
    }

    addNewTodo(model: Itodo) {        
        TodoItems.push(model);
    }

    getTodoItem(id: number): Observable<Itodo> {
        return this.getTodoItems()
            .map((items: Itodo[]) => items.find(p => p.todoId === id));    }
}
like image 502
TTCG Avatar asked Feb 06 '17 22:02

TTCG


People also ask

How do you return an array of objects in TypeScript?

Use the map() method to get an array of values from an array of objects in TypeScript, e.g. const ids = arr. map((obj) => obj.id) . The map method will return a new array containing only the values that were returned from the callback function. Copied!

How do you make observables?

There are various ways to create observables. You can create them from scratch by using Observable constructor (as we saw earlier). You can also create them by using RxJS create function or create them from other data structures such as an array or a promise by using the built-in function of RxJS …and much more.

What is TypeScript Observable?

An Observable is a collection of multiple input values that get processed using array methods such as map , reduce , filter , and so on. It comes in handy when handling asynchronous operations such as making HTTP requests, user-input events, and so on.

Is RxJS a TypeScript?

It's interesting to know that the RxJS project is using TypeScript actively and it helped to find bugs when the library was migrating from JavaScript. In the next section, you can find more articles to learn and keep improving your skills through RxJS.


1 Answers

Observable.of is the correct method you are looking for. On my project, I'm using rxjs5-beta6 and import { Observable } from 'rxjs/Observable'; was enough to use the of method. Depending on your project, you may need to explicitly import the of method.

If all fails, you may try using Observable.from(TodoItems) or even try creating your own observable which return your value.

e.g.

Observable.create((observer: Subscriber<any>) => { observer.next(TodoItems); observer.complete(); });

like image 140
nzjun90 Avatar answered Oct 06 '22 22:10

nzjun90