0

Background

I have an auth.service.ts that when a user logs in it queries the database and returns any information we have on the user. The data is passed in to create a new object of user. It looks like this,

 user: User;
 this.user = res;
 console.log(this.user);

Outputs,

Object account_locked: false affiliate_owner: null business_phone: "8178966767" city_address: null contract: false created_on: null dob: "1984-03-05T06:00:00.000Z" email: "[email protected]" employee_owner: null fax_number: "1234567" first_name: "Nicholas" home_phone: "1234567" id: 7last_name: "theman" middle_name: "dude" mobile_phone: "1234567" ssn: "123456789" state_address: null street_address: null user_auth_level: 1 zip_address: null __proto__: Object

That being true, this...

console.log(this.user.email);

Which outputs [email protected]

The Problem

So when I go to another class and add auth.service.ts to it like this,

import { Auth }                    from './../services/auth.service';

constructor( private router: Router, private auth: Auth, private http: Http  ) { }

This outputs undefined,

ngOnInit() {
      console.log(this.auth.user);
     }

Also from the template I try and use interpolation {{auth.user.email}} which won't work either.

Question

How do I access the object user which is created in auth.service.ts from another class. In this case profile.component.ts and its template profile.component.html

Is this a problem of inheritance?

Class Minimal

    auth.service.ts

    export class Auth {
      lock = new Auth0Lock(myConfig.clientID, myConfig.domain, options, {});
      userProfile: Object;
      logreg: LogReg;
      user: User;
      constructor(private router: Router, private http: Http ) {
            // create users
            this.logreg = new LogReg(profile.email);   
            this.checkRegister(this.logreg).subscribe((res)=>{
                //do something with the response here
                this.user = res;
                console.log(this.user);
            });
    }
}

I want to access that object user here,

profile.component.ts

import { Auth }                    from './../services/auth.service';

@Component({
    providers: [ Auth ],
    templateUrl: './profile.component.html'
})
export class ProfileComponent implements OnInit {

    constructor( private router: Router, private auth: Auth, private http: Http  ) { }

    ngOnInit() {
      console.log(this.auth.user);
     }
}
6
  • please paste a minimal classes definitions to check this out. Commented Nov 29, 2016 at 23:44
  • Which class do you want to see? Commented Nov 29, 2016 at 23:44
  • the auth.service.ts that you want to access from another class like User. Commented Nov 29, 2016 at 23:45
  • I create the user in auth.service.ts im trying to pass it to profile. Commented Nov 29, 2016 at 23:45
  • Did you a export class in your typescript model? Commented Nov 29, 2016 at 23:48

1 Answer 1

1

Supposed to have a class model BaseObject in the file objects.ts like

import * as P from "./protocols"

export class BaseObject implements P.ObjectProtocol {

/** 
     * Fill object by properties dictionary
     * @param properties Object Properties dictionary
    */
    public fill(properties:Object) {
        var self=this;
        for (var i in properties) {
            if (self.hasOwnProperty(i)) {
                self[i]=properties[i];
            }
        }
    } 
}

you are going to export this object using export class.

Where ever you want to use this class you do

import {BaseObject} from "./objects"
export class MyObjectImpl extends BaseObject {
 //...
}

Regarding the issue of sharing the object instance you need a singleton pattern, take a look here: Access key data across entire app in Angular 2 & Ionic 2

Sign up to request clarification or add additional context in comments.

8 Comments

I am exporting the class export class Auth {
Also if I use implments object dont I get more than just the one object I need which has been instantiated in it?
implements OnInit what is OnInit? Should it be implements Auth?
@wuno you need to new that object first.
OnInit is built into angular 2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.