Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firestore保存数据不稳定的结果_Javascript_Angular_Typescript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript Firestore保存数据不稳定的结果

Javascript Firestore保存数据不稳定的结果,javascript,angular,typescript,firebase,google-cloud-firestore,Javascript,Angular,Typescript,Firebase,Google Cloud Firestore,使用firestore实时数据库运行angular 5项目(不是旧的firebase数据库,而是新的) 我有一个非常奇怪的问题,我正在将数据保存到Firestore,当我在我保存的对象上运行Chrome调试时,它正在按预期工作 然而,当我在ChromeOpen中运行相同的进程而没有调试工具时,它只保存了对象的一些属性 我没有以任何方式更改我的代码,我正在使用相同的文件运行相同的进程。简单属性createdAt和updatedAt正在保存,但数据对象未保存的对象数组。除非我在调试模式下运行代码,否

使用firestore实时数据库运行angular 5项目(不是旧的firebase数据库,而是新的)

我有一个非常奇怪的问题,我正在将数据保存到Firestore,当我在我保存的对象上运行Chrome调试时,它正在按预期工作

然而,当我在ChromeOpen中运行相同的进程而没有调试工具时,它只保存了对象的一些属性

我没有以任何方式更改我的代码,我正在使用相同的文件运行相同的进程。简单属性createdAt和updatedAt正在保存,但数据对象未保存的对象数组。除非我在调试模式下运行代码,否则一切正常

非常困惑。欢迎任何意见

应用程序组件

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.$);
                }
              });
            }


});
    }
  });
};
} }

消防商店服务

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();
  }


}
从'@angular/core'导入{Injectable};
进口{
安古拉火炉店,
AngularFirestoreCollection,
角形文件
}来自“angularfire2/firestore”;
从“rxjs/Observable”导入{Observable};
从“firebase/app”导入*作为firebase;
类型CollectionPredicate=string | AngularFirestoreCollection;
类型DocPredicate=string | AngularFirestoreDocument;
@可注射()
出口级消防服务{
colRef:任何;
建造师(
公共afs:AngularFirestore(零售店){
}
col(ref:CollectionPredicate,queryFn?):AngularFirestoreCollection{
返回typeof ref==“string”?this.afs.collection(ref,queryFn):ref;
}
添加(参考:CollectionPredicate,数据){
const timestamp=this.timestamp;
返回此.col(ref).add({
…数据,
更新日期:时间戳,
createdAt:时间戳
});
}
获取时间戳(){
返回firebase.firestore.FieldValue.serverTimestamp();
}
}

为了防止将来有人读到这篇文章,多亏了猎户座的codementor,我终于找到了问题的根源

基本上,这是一个由来已久的异步编程问题。基本上,当我在debug中运行时,我停止程序查看代码,因此异步任务在进入下一步之前已经完成

当不在调试模式下运行时,某些函数在以前的任务完成之前运行,因此无法保存数据

使函数与特定步骤上的等待异步确保了依赖于前一步骤的所有步骤都已完成(作为承诺),确保每次都保存数据