BeepBopImaRobot
BeepBopImaRobot

Reputation: 113

How to pass values to a function that expects a Class

So i have a method for creating a user on my firebase db, It expects a instance of a specific model.

But I have no idea how I take the values combine them and then pass then to the function as that type.

import { Component, OnInit } from '@angular/core';
import { Entry } from '../entry.model';
import { MemberService } from 'src/app/member.service';
import { Member } from 'src/app/member.model';
@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  entry: Entry;
  member: Member;
  constructor(private memberservice: MemberService) { }

  ngOnInit(): void {
  }
//function being called from front end button with input fields that pass values
  create(surName: string, pass: string, color: string){
     //Should call createMember();
     //Id,Priority gets set here not by the user
  }

The Service function:


  createMember(member: Member){
    return this.firestore.collection('members').add(member);
  }

The Model

export class Member {
    id: string;
    surname: string;
    color: string;
    password: string;
    priority: number;
}

Upvotes: 3

Views: 106

Answers (1)

Barremian
Barremian

Reputation: 31125

This is an instance where TS Interface would be a better fit instead of a class. Interface supports type-checking without the unnecessary "bloat" of class.

Try the following

Model

export interface Member {
  id: string;
  surname: string;
  color: string;
  password: string;
  priority: number;
}

You could then create an object of type Member.

export class RegisterComponent implements OnInit {
  ...

  // Function being called from front end button with input fields that pass values
  create(surName: string, pass: string, color: string) {
     const member = {
       id: <set id>,
       surname: surName,
       color: color,
       password: pass,
       priority: <set priority>       
     } as Member;

     this.memberservice.createMember(member)
       .then((res) => console.log('Set member with ID:', id)
       .catch((error) => console.log('Error setting member:', id);
  }
}

Upvotes: 3

Related Questions