从* ngForm检索数据并推送到Firebase

时间:2019-11-11 05:45:55

标签: angular firebase

我有新用户的注册表格。我想将新的用户详细信息推送到Firebase数据库。任何人都可以帮助我。

        <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 个答案:

答案 0 :(得分:1)

您可以这样创建用户服务:

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);
  }

}

并从组件的onSubmit调用createUser()

component.ts

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

component.html

<form class="form-horizontal" [formGroup]="userForm" (ngSubmit)="onSubmit()">
   ...
</form>

答案 1 :(得分:0)

以下方法可帮助我纠正此问题:

user.service.ts

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

这是login.ts文件:

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);
  }