【发布时间】:2018-08-07 00:58:14
【问题描述】:
使用 firestore realtime db(不是旧的 firebase db,新的)运行 angular 5 项目。
我有一个非常奇怪的问题,我将数据保存到 Firestore,当我在我保存的对象上使用 Chrome 调试运行时,我正在按预期工作。
但是,当我在 Chrome 中未打开调试工具的情况下运行相同的进程时,它只会保存对象的一些属性。
我没有以任何方式更改我的代码,我正在使用相同的文件运行相同的进程。简单属性 createdAt 和 updatedAt 正在保存,但数据对象未保存对象数组。除非我在调试模式下运行代码,否则一切正常。
非常困惑。欢迎任何想法。
APP组件
import { Component } from '@angular/core';
import { FirestoreService } from './firestore.service';
import * as xmlLoader from 'xml2js';
import { parseString } from 'xml2js';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 5';
constructor(private fss: FirestoreService) {
}
resultSet = [];
loadXmlFile(files: FileList) {
Array.from(files).forEach(file => {
const myReader: FileReader = new FileReader();
myReader.readAsText(file);
const propertyString = this.removeExtFromFileName(file.name);
this.extractKronosConfiguration(myReader, this.resultSet, propertyString);
});
this.emitKronosConfiguration(this.resultSet);
}
removeExtFromFileName(file): string {
return file.slice(0, -4);
}
emitKronosConfiguration(resultSet) {
this.fss.add('kronos', resultSet);
}
private extractKronosConfiguration(myReader, resultSet, propertyString) {
myReader.onloadend = function (e) {
xmlLoader.parseString(myReader.result, (err, response) => {
resultSet[propertyString] = [];
if (response.Kronos_WFC.Response !== undefined) {
response.Kronos_WFC.Response.forEach(responseItem => {
if (responseItem[propertyString] !== undefined) {
responseItem[propertyString].forEach(item => {
if (item.$ !== undefined) {
resultSet[propertyString].push(item.$);
}
});
}
});
}
});
};
} }
FIRESTORE 服务
import { Injectable } from '@angular/core';
import {
AngularFirestore,
AngularFirestoreCollection,
AngularFirestoreDocument
} from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';
type CollectionPredicate<T> = string | AngularFirestoreCollection<T>;
type DocPredicate<T> = string | AngularFirestoreDocument<T>;
@Injectable()
export class FirestoreService {
colRef: any;
constructor(
public afs: AngularFirestore) {
}
col<T>(ref: CollectionPredicate<T>, queryFn?): AngularFirestoreCollection<T> {
return typeof ref === 'string' ? this.afs.collection<T>(ref, queryFn) : ref;
}
add<T>(ref: CollectionPredicate<T>, data) {
const timestamp = this.timestamp;
return this.col(ref).add({
...data,
updatedAt: timestamp,
createdAt: timestamp
});
}
get timestamp() {
return firebase.firestore.FieldValue.serverTimestamp();
}
}
【问题讨论】:
标签: javascript angular typescript firebase google-cloud-firestore