1

I have signup form for new users. I want to push the new user details to firebase database.Can anyone please help me with it.

        <div class="column" style="padding: 7.5%" >
            <form >
        <label> Full Name </label>
        <input type="text" ng-maxlength="10" hint="Full Name"   class="form-control">
        <label> Phone Number </label>
        <input type="text" hint="phone" id="phone" maxlength="10" minlength="10"  class="form-control">
        
        <label> Aadhar Number </label>
        <input type="text" hint="aadhar" id="aadhar" maxlength="16"  minlength="16" class="form-control">
        <br><br>
        <button id="signup" class="btn btn-primary" routerLink="../../home/dashboard">Signup</button>
    </form>
    </div>

2 Answers 2

1

You can create a user service like this:

service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import * as firebase from 'firebase';
import { IUser } from './IUser';

@Injectable({
  providedIn: 'root'
})
export class UserService {


  constructor(private db: AngularFirestore) { }

  createUser(user: IUser) {
    return this.db.collection('users').add(user);
  }

  getAllUsers(): Observable<any[]> {
    return this.db.collection('users').snapshotChanges();
  }

  getUserById(id) {
    return this.db.doc('users/' + id).get();
  }


  deleteUser(id) {
    return this.db.collection("users").doc(id).delete();
  }

  updateUser(id, user: IUser) {
    return this.db.collection("users").doc(id).set(user);
  }

}

and call createUser() from your component onSubmit:

component.ts

onSubmit(): void {
  this.service.createUser(this.userForm.value).then(
    res => {
    }
  )
}

component.html

<form class="form-horizontal" [formGroup]="userForm" (ngSubmit)="onSubmit()">
   ...
</form>
Sign up to request clarification or add additional context in comments.

7 Comments

in component.ts onSubmit(): void { this.service.createUser(this.userForm.value).then( res => { } ) } For the above snippet..I have declared userForm: any; service: any; But I am getting an error while using "res" error"- res' is declared but its value is never read.ts(6133) Parameter 'res' implicitly has an 'any' type.
You can just use it like var res - res
That didn't work :/ ..I tried declaring it as var res=res;
onSubmit(): void { this.service.createUser(this.userForm.value).then( ( _res: any) => { } ) } I tried this....but the project wouldn't load
Can you create a stackbiltz of your project
|
0

The following method helped me rectify the issue :

user.service.ts

 createUser(data: any) {
    this.users.push(data);
  }

This is the login.ts file:

instituteLogins(instaForm: NgForm){
    console.log("Entered new patient");
    
    console.log(instaForm.value);
    this.addUser(instaForm.value['phoneno'],instaForm.value["patient"],instaForm.value['aadhar']);
    
  }
  
  
  addUser(uid: string, name: string, mobile: string){
    var user = new User();
    user.uid = uid;
    user.name = name;
    user.mobile = mobile;
    this.userService.createUser(user);
  }

Comments

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.