javascript - 可以接收但无法将数据发送到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>
解决方案
错误是抱怨这一行:
this.fs.doc('Printers/').set({ ... })
如果要使用 来创建文档set()
,则必须提供完整路径,包括它所在的集合,例如“Printers/printer_id”。
如果您尝试添加具有随机 ID 的新文档,请使用add()
而不是set()
.
this.fs.collection('Printers').add({ ... })
推荐阅读
- java - Maven 使用不在 POM 中的依赖项进行编译
- node.js - CORS、Laravel Valet 和 Socket.io
- swift - ios 使用 ondeamand 规则重新启动 VPN 连接
- php - 根据购物车页面中的属性值和产品数量在 WooCommerce 中添加到购物车验证
- linux - shell 脚本 - 找到 3 次的平均值
- java - 无法在 Spring Boot 中链接 bean。服务层和控制器 REST
- c# - 尝试将消息从 C# 发布到 Slack 时出现 SSL/TSL 问题
- android - 找不到与包名称“*****.test”匹配的客户端
- vue.js - 如果存在,如何导入 nuxt 组件,否则导入默认组件
- lambda - Java8 lambda流删除对象的两个属性中具有最大值的对象