【问题标题】:angular 6 store data from json array inside json角度6存储来自json数组中的json数据
【发布时间】:2019-01-21 17:44:46
【问题描述】:

我有这个get方法

     this.data.getCoffee(params['id'], response => {
              this.coffee = response;
            });

并且没有响应的是

coffeetype:"frappe"
location:Array(1)
0:{_id: "5b7327a3adcdbc1a80b7861f", address: " adres 5", city: "city5", 
latitude: 21.1211546, longitude: 79.1291045}
 length:1
__proto__:Array(0)
name:"coffee5 "
notes:"note5"
place:"place 5"
rating:7
tastingRating:Array(1)
0:{_id: "5b7327a3adcdbc1a80b7861e", aroma: 6, flavor: 6, intensity: 6, 
aftertaste: 5}
length:1
__proto__:Array(0)
__v:0
_id:"5b7327a2adcdbc1a80b7861d"

但是当我将它保存到 this.coffee = response 的实例时 只有名字、类型、注释和整体风格才能喝到咖啡 品尝数组和位置数组没有进入咖啡的数组实例

咖啡.ts

import { TastingRating } from './TastingRating';
import { PlaceLocation } from './PlaceLocation';

export class Coffee {
 _id: string;
coffeetype: string;

rating: number;
notes: string;
tastingRating: TastingRating;

constructor (
public  name: string = '',
public  place: string= ''  ,
public  location: PlaceLocation = null
) {

  this.location = new PlaceLocation();
  this.tastingRating = new TastingRating();
}
}

地点位置.ts

 constructor(
 public address: string = '',
 public city: string = '',
 public latitude: number = null,
 public longitude: number = null,
  ) {

  }

品尝评级.ts

 export class TastingRating {

   aroma: number;
   flavor: number;
   intensity: number;
   sweetness: number;
   aftertaste: number;

  }

我使用 MongoDB 作为后端 如果我使用 this.coffee.location.city 它显示未定义 甚至 response.location.city 都从 get 方法中说 undefined

【问题讨论】:

  • 请更好地格式化您的帖子。

标签: javascript angular mongodb observable mean


【解决方案1】:

在您的模型 coffee.ts 中,locationtastingRating 不是数组,但在您收到的 JSON 中,这些属性是数组。

locationtastingRating 的类型更改为各自类型的数组,它应该可以工作。

coffee.ts:

import { TastingRating } from './TastingRating';
import { PlaceLocation } from './PlaceLocation';

export class Coffee {
 _id: string;
coffeetype: string;

rating: number;
notes: string;
tastingRating: TastingRating[]; // changed to array

constructor (
public  name: string = '',
public  place: string= ''  ,
public  location: PlaceLocation[] = null // changed to array
) {

  this.location = new Array<PlaceLocation>(); // creating array of PlaceLocation type
  this.tastingRating = new Array<TastingRating>(); // creating array of TastingRating type
}
}

此外,PlaceLocationTastingRating 类中都缺少 _id 属性。将此属性添加到这两个类中。

在您收到的响应中,缺少 TastingRating 类的 sweetness 属性。让响应包含此属性或使其可选,例如 sweetness?: number;

【讨论】:

  • 所以我用这个做了这件事,我不得不像这样 [(ngModel)]="coffee.location[0].address" 将数组值添加到我的 html 代码中,但这给了我错误地址未定义,但值显示在视图中
【解决方案2】:

根据您的回复, response.location 是一个数组。所以你需要寻找确切的数组值

this.coffee.location[0].city

【讨论】:

  • 我用这个方法得到了值,但我现在编译时出错无法读取未定义的属性“城市”
【解决方案3】:

正在检索的用于品尝评级和地点位置的属性与您的数据库的响应不匹配。数据库响应缺少属性甜度的值,并且两个类都没有 _id 值,因此打字稿无法将检索到的值分配给它们。

如果您像在 coffee.ts 中所做的那样向两个类添加一个 _id 字段,并将甜度字段添加到您的数据库中,它应该可以正常工作。

【讨论】:

    【解决方案4】:

    所以考虑到上面的一切,我遇到的问题是我得到了一个用于 testingRating 和 location 的数组。我所做的只是直接从响应中声明这两个具有受人尊敬的数组,如下所示

    this.coffee = response;
    this.coffee.location = response.location[0];
    this.coffee.tastingRating = response.tastingRating[0];                
    

    我不知道这是否是正确的做法,但它工作正常 所以请告诉我你的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2021-12-21
      • 2013-08-08
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多