【问题标题】:Can receive but cant send data to firebase, Code problem?可以接收但无法将数据发送到firebase,代码问题?
【发布时间】:2020-09-20 05:08:25
【问题描述】:

当我尝试将这些值发送到 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>

【问题讨论】:

    标签: javascript html angular firebase google-cloud-firestore


    【解决方案1】:

    错误是抱怨这一行:

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

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

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

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

    【讨论】:

    • 但是id是自动设置的……shell我是怎么做的?
    • 将其添加到您传递给 doc() 的文档路径中,如我在答案中所示。
    • 谢谢你:))))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多