ionic firestore提交数据 [英] ionic firestore submit data

查看:85
本文介绍了ionic firestore提交数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Firestore中提交一些数据,但显示出错误.

I am trying to submit some data in firestore but its showing me error.

FirebaseError: Function CollectionReference.doc() requires its first argument to be of type string, but it was: undefined.

installment.html

installment.html

<ion-content>
  <div [ngSwitch]="galleryType">
    <ion-card *ngSwitchCase="'Form'">
      <ion-item>
        <ion-label floating>Name</ion-label>
        <ion-input type="text" [(ngModel)]="name">
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>Address</ion-label>
        <ion-input type="text" [(ngModel)]="address">
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>CNIC</ion-label>
        <ion-input type="number" [(ngModel)]="cnic">
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>Email</ion-label>
        <ion-input type="email" [(ngModel)]="email">
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>Phone number </ion-label>
        <ion-input type="number" pattern="^([+]92[0-9]{10}|[0-9]{11})$" [(ngModel)]="number">
        </ion-input>
      </ion-item>
      <br>
      <ion-item>
        <ion-label>City</ion-label>
        <ion-select [(ngModel)]="city">
          <ion-option value="karachi">Karachi</ion-option>
          <ion-option value="Lahore">Lahore</ion-option>
          <ion-option value="Multan">Multan</ion-option>
          <ion-option value="islamabad">Islamabad</ion-option>
        </ion-select>
      </ion-item>
      <ion-item>
        <ion-label floating>Profession</ion-label>
        <ion-input type="text" [(ngModel)]="profession">
        </ion-input>
      </ion-item>
      <br>
      <ion-item>
        <ion-label >Select a Bank</ion-label>
        <ion-select [(ngModel)]="bank">
          <ion-option value="HBL">HBL</ion-option>
          <ion-option value="Al Habib">Al Habib</ion-option>
          <ion-option value="SCB">SCB</ion-option>
          <ion-option value="BI">Bank Islami</ion-option>
        </ion-select>
      </ion-item>
      <button style="display:block;margin: 16px auto;" ion-button no-padding (click)="gotoDate()"><img class="tick" src="assets/image/tick.png"></button>
    </ion-card>
    <ion-card *ngSwitchCase="'Date'">
      <ion-item>
        <ion-label floating>Guarantor 1 Name </ion-label>
        <ion-input type="text" [(ngModel)]="g1name" >
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>Phone number </ion-label>
        <ion-input type="number" pattern="^([+]92[0-9]{10}|[0-9]{11})$" [(ngModel)]="g1number">
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>Address </ion-label>
        <ion-input type="text" [(ngModel)]="g1address">
        </ion-input>
      </ion-item>
      <br>
      <ion-item>
        <ion-label>City</ion-label>
        <ion-select [(ngModel)]="g1city">
          <ion-option value="karachi">Karachi</ion-option>
          <ion-option value="lahore">Lahore</ion-option>
          <ion-option value="multan">Multan</ion-option>
          <ion-option value="islamabad">Islamabad</ion-option>
        </ion-select>
      </ion-item>
      <br>
      <ion-item>
        <ion-label floating>Guarantor 2 Name </ion-label>
        <ion-input type="text" [(ngModel)]="g2name">
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>Phone number </ion-label>
        <ion-input type="text" pattern="^([+]92[0-9]{10}|[0-9]{11})$" [(ngModel)]="g2number">
        </ion-input>
      </ion-item>
      <ion-item>
        <ion-label floating>Address </ion-label>
        <ion-input type="text"[(ngModel)]="g2address" >
        </ion-input>
      </ion-item>
      <br>
      <ion-item>
        <ion-label>City</ion-label>
        <ion-select [(ngModel)]="g2city">
          <ion-option value="karachi">Karachi</ion-option>
          <ion-option value="lahore">Lahore</ion-option>
          <ion-option value="multan">Multan</ion-option>
          <ion-option value="islamabad">Islamabad</ion-option>
        </ion-select>
      </ion-item>
      <button style="display:block;margin: 16px auto;" ion-button no-padding><img class="tick" src="assets/image/tick.png" (click)="submit(name, address, cnic, email, number, city, profession, bank, g1name, g1number, g1address, g1city, g2name, g2address, g2number, g2city) "></button>
    </ion-card>
  </div>
</ion-content>

.ts

submit(name, address, cnic, email, number, city, profession, bank, g1name,
  g1number, g1address, g1city, g2name, g2address, g2number, g2city) {
  if (this.name == "") {
    console.log('empty');
    return;
  } else {
    let deal = {
      name: this.name,
      address: this.address,
      cnic: this.cnic,
      email: this.email,
      number: this.number,
      city: this.city,
      profession: this.profession,
      bank: this.bank,
      // Guarantor1name: this.g1name,
      g1number: this.g1number,
      g1address: this.g1address,
      g1city: this.g1city,
      //Guarantor2name: this.g2name,
      g2number: this.g2number,
      g2address: this.g2address,
      g2city: this.g2city,
    }

    this.afs.collection('installment').doc().set(deal);
    let alert = this.alertCtrl.create({
      title: "ATTENTION",
      subTitle: "Done",
      buttons: ['OK']
    });
    alert.present();
  }
}

我需要将数据发送到Firestore.有人可以告诉我如何发送吗?

I need to send the data to firestore. Any one can please tell me how can I send it?

推荐答案

看起来您正在尝试创建一个新文档,但是当前,您的代码未设置要创建的文档ID.一个建议是让Cloud Firestore使用自动ID.

Looks like you are trying to create a new document, but currently, your code doesn't set a document id to be created. One suggestion is to just let Cloud Firestore use an auto id.

为此,请更改此行:

this.afs.collection('installment').doc().set(deal);

对此:

this.afs.collection('installment').add(deal);

这篇关于ionic firestore提交数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆