首页 > 解决方案 > 可以接收但无法将数据发送到firebase,代码问题?

问题描述

当我尝试将这些值发送到 Firebase 时,我收到此错误:“无效的文档引用。文档引用必须有偶数个段,但打印机有 1 个”错误发布下面代码的图片也许有人可以摆脱对此有所了解,非常感谢!

Typescript:

import { Component, OnInit } from '@angular/core';
import { PrintersService } from '../printers.service';


@Component({
  selector: 'app-addprinter',
  templateUrl: './addprinter.component.html',
  styleUrls: ['./addprinter.component.css']
})
export class AddprinterComponent implements OnInit {

  constructor(private printersService: PrintersService) { }

  ngOnInit() {

  }


  AddPrinter(form) {

    this.printersService.AddPrinter(
      form.value.hostName,
      form.value.ip,
       form.value.location,
        form.value.manufacturer,
         form.value.model,
          form.value.specificLocation).then(
    data => console.log(data))
    .catch(err => console.log(err));
    console.log(form);
  }
}
Service:

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';

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

  constructor(private fs: AngularFirestore) { }

  getAllPrinters() {
    return this.fs.collection('Printers').valueChanges();
  }

  AddPrinter(HostName, Ip, Location, Manufacturer, Model, SpecificLocation) {
    return this.fs.doc('Printers/').set({
      HostName,
      Ip,
      Location,
      Manufacturer,
      Model,
      SpecificLocation

    });

  }
}
HTML:

<br><br><br><br>
<h2 class="text-center">Add Printer</h2>

<form #f="ngForm" (ngSubmit)="AddPrinter(f)">

<input ngModel name="hostName" #hostname="ngModel" type="text" class="formControl" required placeholder="Enter HostName here">
<div class="alert alert-danger" *ngIf="hostname.touched &&hostname.errors?.required">The HostName is requied</div>


<input ngModel name="ip" #ip="ngModel" type="text" class="formControl" required placeholder="Enter Ip here">
<div class="alert alert-danger" *ngIf="ip.touched &&ip.errors?.required">The Ip is requied</div>


<input ngModel name="location" #location="ngModel" type="text" class="formControl" required placeholder="Enter Loctaion here">
<div class="alert alert-danger" *ngIf="location.touched &&location.errors?.required">The Location is requied</div>


<input ngModel name="manufacturer" #manufacturer="ngModel" type="text" class="formControl" required placeholder="Enter Manufacturer here">
<div class="alert alert-danger" *ngIf="manufacturer.touched &&manufacturer.errors?.required">The Manufacturer is requied</div>


<input ngModel name="Model" #model="ngModel" type="text" class="formControl" required required placeholder="Enter Model here">
<div class="alert alert-danger" *ngIf="model.touched &&model.errors?.required">The Model is requied</div>


<input ngModel name="specificLocation" #specificLocation="ngModel" type="text" class="formControl" required placeholder="Enter Specific Location here">
<div class="alert alert-danger" *ngIf="specificLocation.touched && specificLocation.errors?.required">The Specific Location is requied</div>

<button class="btn btn-primary" [disabled]="f.invalid">Send</button>

</form>

标签: javascripthtmlangularfirebasegoogle-cloud-firestore

解决方案


错误是抱怨这一行:

this.fs.doc('Printers/').set({ ... })

如果要使用 来创建文档set(),则必须提供完整路径,包括它所在的集合,例如“Printers/printer_id”。

如果您尝试添加具有随机 ID 的新文档,请使用add()而不是set().

this.fs.collection('Printers').add({ ... })

推荐阅读