3

I have a Django REST API with an Angular Front-end. I am getting no errors on the front-end, but no json data is logging to the console.

I have a service like so:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class WeatherstatsService {

  constructor(private http:Http) {
    console.log("Hello World");

    this.getWeatherData();
    // this.getWeatherDataRecords();
  }

  weatherdata: any = {};

  private _WeatherStatsUrl = 'http://127.0.0.1:8000/weatherstats/weatherdata';

  getWeatherData() {
    return this.http.get(this._WeatherStatsUrl)
      .map((res:Response) => res.json());
  }

  getWeatherDataRecords() {
    this.getWeatherData().subscribe(weatherdata => {
      console.log(weatherdata)
    })
  }
}

feeding a component like so:

import { Component, OnInit } from '@angular/core';
import { WeatherstatsService } from '../weatherstats.service';

@Component({
  selector: 'weather-stats',
  templateUrl: './weather-stats.component.html',
  styleUrls: ['./weather-stats.component.css']
})
export class WeatherStatsComponent implements OnInit {

  data: any;

  constructor(private weatherstatsservice: WeatherstatsService) { }

  ngOnInit() {

    console.log(this.weatherstatsservice.getWeatherData().subscribe(data => this.data = data));

  }

}

I am new to both Django and Angular, when I test the API in the browser I get the data I want. What could be happening that means I can't see my json data? (With apologies to people who tried to help on similar issue just now - My brain is exploding with all this new stuff!)

Console:

Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}
    closed
    :
    false
    destination
    :
    SafeSubscriber {closed: false, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, …}
    isStopped
    :
    false
    syncErrorThrowable
    :
    false
    syncErrorThrown
    :
    false
    syncErrorValue
    :
    null
    _parent
    :
    null
    _parents
    :
    null
    _subscriptions
    :
    Array(1)
    0
    :
    MapSubscriber {closed: true, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, …}
    length
    :
    1
    __proto__
    :
    Array(0)
    __proto__
    :
    Subscription
7
  • Is angular app and django running in different port? Commented Nov 30, 2017 at 18:20
  • Ah yeah! I guess that's bad? Or is it good? One is running on port 8000 and the other on port 4200 Commented Nov 30, 2017 at 18:22
  • any errors in console? Commented Nov 30, 2017 at 18:40
  • No errors as such but will post console feed Commented Nov 30, 2017 at 18:42
  • The terminal feed for the django server says: [30/Nov/2017 18:36:37] "GET /weatherstats/weatherdata/ HTTP/1.1" 200 6365027 so I'm assuming it is receiving the request and serving the data Commented Nov 30, 2017 at 18:44

1 Answer 1

4

You are now console logging the subscription, so the output you are getting in your console is correct. To console log the response, do:

this.weatherstatsservice.getWeatherData()
  .subscribe(data => {
    this.data = data;
    console.log(this.data)
  })
Sign up to request clarification or add additional context in comments.

1 Comment

and if this is new stuff I can suggest reading of this: stackoverflow.com/questions/43055706/… A very common pitfall is to try and reach data outside of subscribe, so a heads up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.