简体   繁体   English

为什么在我的控制台中有这个:angular.js:13708 TypeError:无法设置未定义的属性“ projects”

[英]Why have I in my console this: a angular.js:13708 TypeError: Cannot set property 'projects' of undefined

I am trying to fetch data returning from http promise received by three components to put it into a third one, here's my code (it will be easiest to understand). 我试图获取从三个组件收到的http承诺返回的数据,然后将其放入第三个组件,这是我的代码(最容易理解)。

I am trying to $onInit on the component to bind thoses datas in the view but I can't see anything in the html. 我正在尝试在组件上绑定$ onInit以在视图中绑定这些数据,但在html中看不到任何内容。

I really don't understand why my property 'project' is undefied because she's part of the Admin2 interface..? 我真的不明白为什么我的属性“项目”没有定义,因为她是Admin2界面的一部分。

Still I have got a way to go, I know I need to instanciate my admin2 object in my consctructor a little bit as follow: 我还有路要走,我知道我需要在构造器中实例化我的admin2对象,如下所示:

 constructor(private baseURL: string, 
            private Shared: SharedService, 
            private Auth: AuthService, 
            private $state: IStateService
            ) { 
    'ngInject'; 
    this.Shared.title = 'ADMIN2'; 
    this.allowedRoles = ['admin']
    this.currentLocale = localStorage.getItem('locale');
    this.admin2 = {
        projects: {
            id: '',
            name: '',
            start_date: ''
        },
        consultants: {

                        },
        newsletters: {

                },
     };

here's the rest of the code: 这是其余的代码:

 import * as angular from 'angular';
 import {IPromise} from "angular";
 import {IStateService} from 'angular-ui-router';
 import {IPaginationResponse} from "../../models/pagination/pagination-response";
import {Component} from '../../decorators/component';

import {AuthService} from "../../services/auth.service";
import {SharedService} from "../../services/shared.service";
import {ProjectService} from "../../services/project.service";
import {NewsletterService} from '../../services/newsletter.service';
import {ConsultantService} from "../../services/consultant.service";

import {IAdmin2} from "../../models/admin2";
import {IProject} from "../../models/project";
import {INewsletter} from "../../models/newsletter";
import {IConsultant} from "../../models/consultant";


@Component({
   templateUrl: '/app/src/components/admin2/admin2.component.html',
   bindings: {
    projectPagination: '<',
    consultantPagination: '<',
    newsletterPagination: '<',
    }
})
export class Admin2Component {
   public search: any = {searchInFile: 0}; 
   public promise: IPromise<void>; 
   public pagination: any = { 
    projectPagination: {
        order: 'start_date', 
        limit: 25, 
        page: 1 
    },
    consultantPagination: {
        order: 'trigram', 
        limit: 25, 
        page: 1 
    },
    newsletterPagination: {
        order: 'release_date', 
        limit: 25, 
        page: 1 
      }
   }; 

    // Bindings 
   public currentLocale: string; 
    public allowedRoles: string[]; 
   public admin2: IAdmin2; 

   public projectPagination: IPaginationResponse<IProject>; 
   public consultantPagination: IPaginationResponse<IConsultant>; 
    public newsletterPagination: IPaginationResponse<INewsletter>; 

    constructor(private baseURL: string, 
            private Shared: SharedService, 
            private Auth: AuthService, 
            private $state: IStateService) { 
    'ngInject'; 
       this.Shared.title = 'ADMIN2'; 
      this.allowedRoles = ['admin']
      this.currentLocale = localStorage.getItem('locale');
}

        private updateList(projectPagination: 
IPaginationResponse<IProject>, consultantPagination: 
IPaginationResponse<IConsultant>, newsletterPagination: 
IPaginationResponse<INewsletter>)   {


       this.admin2.projects = projectPagination.data;
       this.pagination.projectPagination = projectPagination.meta;
       this.pagination.total = projectPagination.meta.pagination.total;
       this.pagination.page = projectPagination.meta.pagination.current_page;

    this.pagination.consultantPagination = consultantPagination.meta;
    this.pagination.total = consultantPagination.meta.pagination.total;
    this.pagination.page = consultantPagination.meta.pagination.current_page;

    this.pagination.total = newsletterPagination.meta.pagination.total;
    this.pagination.page = 
  newsletterPagination.meta.pagination.current_page;    


   console.log(this.pagination.projectPagination);
   console.log(this.pagination.consultantPagination);


  $onInit() {



                this.updateList(this.projectPagination, 
 this.consultantPagination, this.newsletterPagination);



   }

}

Also here is my interface IAdmin2: 这也是我的界面IAdmin2:

 import {IProject} from "./project";
 import {IConsultant} from "./consultant";
 import {INewsletter} from "./newsletter";

 export interface IAdmin2  {
    projects: IProject[];
    consultants: IConsultant[];
    newsletters: INewsletter[];
 }

For info (property) Admin2component.admin: IAdmin2 and IAdmin.projects: IProject[] (if it can help uno) 有关信息(属性)Admin2component.admin:IAdmin2和IAdmin.projects:IProject [](如果可以帮助您)

Here's my full error message: 这是我的完整错误消息:

 TypeError: Cannot set property 'projects' of undefined
   at Admin2Component.updateList (admin2.component.ts:77)
   at Admin2Component.$onInit (admin2.component.ts:130)
   at angular.js:9179
   at forEach (angular.js:329)
   at nodeLinkFn (angular.js:9168)
   at angular.js:9553
   at processQueue (angular.js:16170)
   at angular.js:16186
   at Scope.$eval (angular.js:17444)
   at Scope.$digest (angular.js:17257)
   at Scope.$apply (angular.js:17552)
   at done (angular.js:11697)
   at completeRequest (angular.js:11903)
   at XMLHttpRequest.requestLoaded (angular.js:11836)
   at XMLHttpRequest.wrapFn [as __zone_symbol___onload] (zone.js:1242)

You need to use Oninit Hook by invoking your services in using observables 您需要通过使用可观察对象调用服务来使用Oninit Hook

https://angular.io/api/core/OnInit https://angular.io/api/core/OnInit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道为什么我会收到以下消息:“ ERROR TypeError:无法设置未定义的属性&#39;position&#39;” - I don't know why I receive this message in angular: “ERROR TypeError: Cannot set property 'position' of undefined” Angular 2-TypeError:无法设置未定义的属性“ userId” - Angular 2 - TypeError: Cannot set property 'userId' of undefined Angular TypeError 无法设置未定义的属性“数据” - Angular TypeError cannot set property 'Data' of undefined 我的控制台中的错误=&gt;(core.js:6456 错误类型错误:无法读取未定义的属性'doc') - Error in my Console=> (core.js:6456 ERROR TypeError: Cannot read property 'doc' of undefined) 控制台中的角度组件错误:错误TypeError:无法读取未定义的属性“ 1” - Angular Component error in the console: ERROR TypeError: Cannot read property '1' of undefined 我有 angular.min.js:129 TypeError: Cannot read properties of undefined (reading '0') error in my spring boot application - i have angular.min.js:129 TypeError: Cannot read properties of undefined (reading '0') error in my spring boot aplication Angular:Typescript:未捕获类型错误:无法设置未定义的属性“autoTable” - Angular: Typescript: Uncaught TypeError: Cannot set property 'autoTable' of undefined 在我的 Angular 表单中获取“类型错误:无法读取未定义的属性‘错误’” - Getting 'TypeError: Cannot read property 'errors' of undefined' in my Angular Form 错误类型错误:无法设置未定义 ANGULAR 的属性“名称” - Error TypeError: cannot set property “name” of undefined ANGULAR angular2 TypeError:无法设置undefined的属性“name” - angular2 TypeError: Cannot set property 'name' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM