1

I have an Angular 5 app which gets its data through a Rails API (referenced through localhost:3000). I receive a response just like I intend to, But the response is missing essential contents. In the response object, key value pairs are missing, not just the value associated with the keys.

I thought it was a back-end issue, but running a test script on it reveals that it is in fact sending out the data it is supposed to, and I am wondering where something might be going wrong.

In my component, sitelist.component.ts

import { Component, Inject, OnInit, Input } from '@angular/core';
import { DataService } from '../data.service';
import {  Response } from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { AuthService } from "../services/auth.service";

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

export class SitelistComponent implements OnInit {
  sites;
  originals;
  this_site

constructor(private http: HttpClient, private _dataService: DataService, public authService: AuthService ) {}

ngOnInit() {

this.authService.getSites()
.map((res:Response) => (
  <any>res.json() 
 ))
.subscribe(data => {
   const copies = [];
   for(let item of data) {
     this.copy = this.copy(item);
     copies.push(this.copy);
   }
   this.originals = data;
   this.sites = copies;

 })
}

copy(Object: any):any{
  console.log(JSON.parse(JSON.stringify(Object)))
  return JSON.parse(JSON.stringify(Object))
 }
}

The 'getSites' request in the service, auth.service.ts:

import { Injectable, OnInit } from '@angular/core';
import {Angular2TokenService} from "angular2-token";
import {Subject, Observable} from "rxjs";
import {Response} from "@angular/http";
import 'rxjs/add/operator/map';
import { HttpClient } from '@angular/common/http';
import { Router } from "@angular/router";
import { BehaviorSubject } from 'rxjs';
import { ThrowStmt } from '@angular/compiler';

@Injectable()
export class AuthService {
  current_route: string;
  current_id: string;
  route_with_id: string
  current_restructured_mp;

  userSignedIn$:Subject<boolean> = new Subject();

  constructor(public authService:Angular2TokenService,private router:Router,private http: HttpClient) {

    this.authService.validateToken().subscribe(

    res =>  res.status == 200 ? 
    this.userSignedIn$.next(res.json().success) : 
    this.userSignedIn$.next(false)
    )
  }

  getSites(){ 
    return this.authService.get('sites.json')
  }

I copy the response object in sitelist.component.ts as 'copies' so that any modification/parsing/etc that I pass through to the children doesn't affect it. When I console.log it I am receiving:

{
"address": null
"alerts": {count: 0, anomalies: Array(0)}
"device_count": 0
"historical_data": 
  {
   2018-09-30: {"anomalies": null},
   2018-09-29: {"anomalies": null}, 
  }
"id": 1
"latitude": "39.0"
"longitude": "-104.0"
"name": "Test Site"
}

But I should be receiving '"precipitation": num' alongside each of the 'anomalies' under historical data. Is there something I am doing wrong in my service or something that would cause this partial lack of response?

Thank you! Please let me know if I can provide anymore information.

EDIT: The key/value pairs that I am missing are sourced from an api other than the back-end api. It might be that I don't get the data from it by the time it is evaluated?

Devstar34
  • 1,027
  • 2
  • 21
  • 47
  • 1
    Are you using HttpClient in the `AuthService` as well? If so, you don't have to `map` its response by calling `json` on a returned value from one of its methods. – SiddAjmera Oct 08 '18 at 20:55
  • possibly helpful ... https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays – danday74 Oct 08 '18 at 20:56
  • @SiddAjmera It's injected in my service but I don't think it's being utilized, would I write 'return this.http.get('sites.json')' in my service differently in order to use it? – Devstar34 Oct 08 '18 at 21:01

0 Answers0