Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class User {
- user: any;
- constructor() {
- }
- getAge() {
- return "Age"
- }
- }
- import { UserService } from '../../services/user.service';
- import { AuthService } from '../../services/auth.service';
- import { Component, OnInit } from '@angular/core';
- import { Router, ActivatedRoute, ParamMap } from '@angular/router';
- import 'rxjs/add/operator/switchMap';
- import {User} from '../../models/user.model'
- @Component({
- selector: 'app-profile',
- templateUrl: './profile.component.html',
- styleUrls: ['./profile.component.scss']
- })
- export class ProfileComponent implements OnInit {
- user: User;
- constructor(private authService: AuthService,
- private route: ActivatedRoute,
- private router: Router,
- private userService: UserService) { }
- ngOnInit() {
- this.route.params.switchMap((params) => {
- let user_id = params['id'];
- return this.userService.get(user_id);
- }).subscribe((res) => {
- this.user = res;
- let age = this.user.getAge();
- console.log(age);
- });
- }
- }
- import { Injectable } from '@angular/core';
- import { ApiService } from './api.service';
- @Injectable()
- export class UserService {
- path = 'users/';
- constructor(
- private apiService: ApiService
- ) {
- }
- all() {
- return this.apiService.get(this.path);
- }
- create(user) {
- return this.apiService.post(this.path, user);
- }
- get(user_id) {
- let endpoint = this.path + user_id;
- return this.apiService.get(endpoint);
- }
- }
- let UserSchema = new Schema({
- email: {
- address: {
- type: String,
- lowercase: true,
- //unique: true,
- },
- token: String,
- verified: {
- type: Boolean,
- default: false,
- },
- },
- password: {
- type: String,
- },
- socketId: String,
- isOnline: Boolean,
- phone: {
- countryCode: {
- type: String,
- },
- number: {
- type: String,
- },
- code: String,
- verified: {
- type: Boolean,
- default: false
- },
- },
- jwt: String,
- profile: {
- username: String,
- firstname: String,
- lastname: String,
- dob: String,
- gender: String,
- level: Number,
- location: {
- longitude: String,
- latitude: String
- },
- image: String,
- introduction: String,
- following: [],
- followers: [],
- },
- },
- {
- timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
- });
- public get(user_id): Observable<User> {
- let endpoint = this.path + user_id;
- return this.apiService.get(endpoint).map(res => {
- let userObj = new User();
- userObj.user = res;
- return userObj;
- });
- }
- this.route.params.switchMap((params) => {
- let user_id = params['id'];
- return this.userService.get(user_id);
- }).subscribe((res) => {
- let userRef: User = new User();
- Object.assign(userRef, res);
- this.user = userRef;
- let age = this.user.getAge();
- console.log(age);
- });
- export class User {
- user: any;
- constructor() {}
- getAge(){ return "Age"}
- public static fromObject(obj):User {
- let userRef: User = new User();
- Object.assign(userRef, obj);
- return userRef;}}
- this.route.params.switchMap((params) => {
- let user_id = params['id'];
- return this.userService.get(user_id);
- }).subscribe((res) => {
- this.user = User.fromObject(res);
- let age = this.user.getAge();
- console.log(age);
- });
- get(user_id) {
- let endpoint = this.path + user_id;
- return this.apiService.get(endpoint).map(res => new User(){//set your user properties here});
- }
Add Comment
Please, Sign In to add comment