Angular 5中API响应中是否缺少数据?

时间:2018-10-08 20:53:41

标签: angular http rails-api

我有一个Angular 5应用,该应用通过Rails API(通过localhost:3000引用)获取其数据。我收到的回复与预期的一样,但是该回复缺少必要的内容。在响应对象中,缺少键值对,而不仅仅是与键关联的值。

我认为这是一个后端问题,但是在其上运行测试脚本则表明它实际上正在发送应该发送的数据,我想知道哪里出了问题。

在我的组件中, 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))
 }
}

服务 auth.service.ts中的“ getSites”请求:

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')
  }

我将响应对象复制到sitelist.component.ts中作为“副本”,以便我传递给子代的任何修改/解析/等都不会影响它。当我console.log时,我收到:

{
"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"
}

但是我应该在历史数据下的每个“异常”旁边都收到“降水”:num。我的服务有问题吗,还是会导致部分缺乏响应?

谢谢!如果可以提供更多信息,请告诉我。

编辑:我缺少的键/值对源自后端api之外的api。可能是在评估时我还没有从中获得数据?

0 个答案:

没有答案