0

I'm new to Angular and I'm creating a test Application to accelerate my understanding of the topic. Recently I encountered a challenge to integrate Angular2(FrontEnd) with Django(Backend) by fetching the data using REST APIs.

File: library.service.ts

import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Rx';
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
// Project Modules
import { Library } from '../models';


@Injectable()
export class LibraryService {
  private librariesUrl = 'http://127.0.0.1:8000/api/library/create-library/';
  constructor(private http: Http) { }
  private headers = new Headers({'Content-Type': 'application/json'});
  private extractData(res: Response) {
   return res.json();
  }
  private handleError (error: any) {
    return Observable.throw(error.message || 'Server error');
  }
  getAll(): Observable<Library[]> {
    return this.http.get(this.librariesUrl, {headers: this.headers})
      .map((res) => this.extractData(res.json())).catch((err) => this.handleError(err));
  }
}

File: libraries.component.ts

import { Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
// Project Modules
import { Library } from '../models';
import { LibraryService } from './library.service';

@Component({
  selector: 'app-libraries',
  templateUrl: './libraries.component.html',
  styleUrls: ['./libraries.component.css'],
})

export class LibrariesComponent implements OnInit {
  libraries: Library[];
  personalLibraries: Library[];
  collaborativeLibraries: Library[];
  constructor(private libraryService: LibraryService, private http: HttpClient) { }
  ngOnInit(): void {
    /*this.http.get('http://127.0.0.1:8000/api/library/create-library/').subscribe((data: Library[]) => {
      console.log(data);
      this.personalLibraries = data;
    });*/
    this.libraryService.getAll().subscribe(response => this.personalLibraries = response);
  }
}

REST API

# Django Modules
from django.shortcuts import get_object_or_404
# REST Modules
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.decorators import api_view, authentication_classes, permission_classes
# Project Modules
from .models import Resource, ResourceUserAssociation, Collection, Library
from mysite.utils import get_value_or_404, get_value_or_default, get_boolean
from .serializers import LibrarySerializer, CollectionSerializer


# TODO: Check user authentication here

class CreatorLibraryAPI(APIView):
    def get(self, request, format=None):
        # slug = get_value_or_404(request.GET, 'slug')
        lib_object = Library.objects.filter(type='personal')
        sdata = LibrarySerializer(lib_object, many=True).data
        return Response(sdata, status=status.HTTP_200_OK)

JSON I'm Expecting

[
    {
        "slug": "tech-stack",
        "title": "Technology Stack",
        "description": "Library of resources related to Technology",
        "type": "personal"
    },
    {
        "slug": "biz-stack",
        "title": "Technology Stack",
        "description": "Library of resources related to Business",
        "type": "personal"
    },
    {
        "slug": "design-stack",
        "title": "Design Stack",
        "description": "Library of resources related to Design",
        "type": "personal"
    }
]

Important When I try to fetch data in the Component only, then I successfully get the result [See the commented code in libraries.components.ts]. But somehow it's not working in the Service, am I doing something wrong with Observables?

Note This problem is very similar to Question here.

Big thanks to the community in advance :)

georgeawg
  • 48,608
  • 13
  • 72
  • 95

1 Answers1

0

Few changes I've made: Used HttpClient instead of Http. This allows me to remove .map() as HttpClien already returns the JSON (instead of the whole response).

Correct File: library.service.ts

import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Rx';
import { Injectable } from '@angular/core';
import { Headers } from '@angular/http';
import {HttpClient} from '@angular/common/http';
// Project Modules
import { Library } from '../models';


@Injectable()
export class LibraryService {
  private librariesUrl = 'http://127.0.0.1:8000/api/library/create-library/';
  constructor(private http: HttpClient) { }
  private headers = new Headers({'Content-Type': 'application/json'});
  private handleError (error: any) {
    console.log('---------- CATCH ERROR ----------');
    return Observable.throw(error.message || 'this is some random server error');
  }

  getAll(): Observable<Library[]> {
    return this.http.get(this.librariesUrl).catch((err) => this.handleError(err));
  }
}