发生错误TypeError:无法设置undefined的属性'tenant'

时间:2017-08-03 10:13:02

标签: javascript angular scope

我正在尝试在setTenantData方法中设置租户的值,但是它给出了错误 发生错误TypeError:无法设置未定义的属性'tenant'

我理解它的范围问题,但是如何解决这个问题。 我试过将'this'分配给一个变量,但仍然无效。

import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/toPromise';
import { TenantModel } from '../models/tenant-model';
import { NgAuthService } from './ng-auth.service';



@Injectable()
export class TenantService {



    appConstants: any;
    authObj: any;
    baseUrl: any;
    tenantUrl: any = '';
    tenant: object = {};



    constructor(private http: Http, public ngAuth: NgAuthService) {

    }

    setTenant() {
        this.authObj = this.ngAuth.getNgAuth();
        this.appConstants = this.ngAuth.getAppConstant();
        this.baseUrl = this.authObj.apiServiceBaseUri;
        this.tenantUrl = this.baseUrl + 'tenant/validate';
        var data = { 'DomainName': this.appConstants.hostName };
        var headers = new Headers();
        headers.append('Content-Type', 'application/json');

        this.http.post(this.tenantUrl, data, { headers:headers, method:'POST' })
            .toPromise()
            .then(this.setTenantData)
            .catch(this.handleError);



    }

    public setTenantData(tenantData: any) {
        console.log("setTenantData");
        this.tenant = tenantData.json().tenant;
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }

    getTenant() {

        return this.tenant;
    }

}

0 个答案:

没有答案