如何在angular / drupal专案中加入与angular2-jsonapi的关系

时间:2019-07-08 14:36:18

标签: angular drupal

我无法在findRecord()方法中包含关系。我已经有相同的错误:

  

parseHasMany-关系节点的模型类型-放置不   找到。

我不明白为什么?

我的dataStore.service

    import { Injectable } from '@angular/core';
    import { JsonApiDatastoreConfig, JsonApiDatastore, DatastoreConfig  } from 'angular2-jsonapi';

    import { HttpClient } from '@angular/common/http';
    import { environment } from '../../../environments/environment';

    import { Etablissement } from '@models/etablissement.model';
    import { Emplacement } from '@models/emplacement.model';
    import { Page } from '@models/page.model';


    const config: DatastoreConfig = {

      baseUrl: environment.api.baseUrl + 'jsonapi',
      models: {
      page: Page,
      emplacement: Emplacement,
      etablissement: Etablissement

      }
    };

    @Injectable({
      providedIn: 'root'
    })
    @JsonApiDatastoreConfig(config)
    export class DatastoreService extends JsonApiDatastore{

      constructor(http: HttpClient) {
        super(http);
       }
    }

我的etablissement.model

import { JsonApiModelConfig, JsonApiModel, Attribute, HasMany, BelongsTo } from 'angular2-jsonapi';

import { Emplacement } from '@models/emplacement.model';

@JsonApiModelConfig({
    type: 'node/etablissement'
})
export class Etablissement extends JsonApiModel{
    @Attribute()
    title: string;

    @Attribute()
    field_key: string;

    @Attribute()
    field_commentaires: string;

    @Attribute()
    field_description: string;

    @Attribute()
    field_ess: boolean;

    @Attribute()
    field_mail: string;

    @Attribute()
    field_monnaies: string;

    @Attribute()
    field_ref_categories: string;

    @HasMany()
    field_ref_emplacement: Emplacement[];

    @Attribute()
    field_label: string;

    @Attribute()
    field_verifie: boolean;
}

我的emplacement.model

import { JsonApiModelConfig, JsonApiModel, Attribute, HasMany, BelongsTo } from 'angular2-jsonapi';

@JsonApiModelConfig({
    type: 'node/emplacement'
})
export class Emplacement extends JsonApiModel{
    @Attribute()
    title: string;

    @Attribute()
    body: string;

    @Attribute()
    field_coordonnee: Geolocation;

    @Attribute()
    field_horaires: string;

    @Attribute()
    field_telephone: string;

    @Attribute()
    field_nom_emplacement: string;

}

和我的企业组件

import { Component, OnInit } from '@angular/core';
import { ErrorResponse } from "angular2-jsonapi";
import { DatastoreService } from '@app/shared/services/datastore.service';
import { Etablissement } from '@models/etablissement.model';

@Component({
  selector: 'app-entreprise',
  templateUrl: './entreprise.component.html',
  styleUrls: ['./entreprise.component.scss']
})
export class EntrepriseComponent implements OnInit {

  etablissement : any = false;

  constructor(private datastore: DatastoreService) { }

  ngOnInit() {

      this.getEnt('7338dfea-9bd2-4c61-b8be-df4ed1493c19');


  }

  getEnt(uuid: string){

    this.datastore.findRecord(Etablissement, uuid, {
      include: 'field_ref_emplacement'
    })
    .subscribe(
      (etablissement: Etablissement) => {

        console.log(etablissement);
      },
      (error:  ErrorResponse) => {

          console.log(error);
        }

    );
  }
}

(对不起,这是我第一次来!我的英语也很法语!)

1 个答案:

答案 0 :(得分:0)

您必须在DatastoreConfig上设置正确的类型

类似的东西:

const config: DatastoreConfig = {
  baseUrl: environment.api.baseUrl + 'jsonapi',
  models: {
    'node--page': Page,
    'node--emplacement': Emplacement,
    'node--etablissement': Etablissement
  }
};