我已经问过我的同事这个问题,但是他们无法提出解决方案,而且我已经为这个问题研究了好几天。当我加载该程序并且不对数据库执行任何操作时,该程序运行良好,但是当我在其中添加一些信息时,该数据库不会在应用程序上加载,并且会引发错误“ _this.documents.sort is not功能”。另外,我已经编写了一个console.log进行调试,它表明我从数据库中提取数据很好。当我删除该行时,它表示我的this.documents.slice()
也是错误的。
如果有人好奇,这是我的火力发源地:https://cmsproject-4163e.firebaseio.com/
这是我得到错误的地方(注意:这只是我的代码的一部分,由于这些都是引发错误的类,因此我不会向您展示所有代码):
我的documents.service.ts文件:
import { Injectable, EventEmitter } from '@angular/core';
import { MOCKDOCUMENTS } from './MOCKDOCUMENTS';
import { Subject } from 'rxjs';
import { Document } from './document.model';
import {HttpClient, HttpHeaders, HttpResponse } from'@angular/common/http'
@Injectable({
providedIn: 'root'
})
export class DocumentsService {
documentSelectedEvent = new EventEmitter<Document>();
documentListChangedEvent = new Subject<Document[]>();
maxDocumentId: number;
documents: Document[] = [];
id: string;
constructor(private http: HttpClient, private documentService: DocumentsService) {
this.documents = MOCKDOCUMENTS;
this.maxDocumentId = this.getMaxId();
}
getDocuments() {
this.http.get('https://cmsproject-4163e.firebaseio.com/documents.json')
.subscribe(
(responseData: Document[]) => {
this.documents = responseData;
console.log(this.documents);
this.documents.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
this.documentListChangedEvent.next(this.documents.slice());
}
)
}
//Add document function
addDocument(document: Document) {
if(!document){
return;
}
document.id = '';
const headers = new HttpHeaders({'Content-Type':'application/json'});
this.http.post<{ message: string, document:Document}>('https://cmsproject-4163e.firebaseio.com/documents.json',
document,
{ headers: headers })
.subscribe(
(responseData) => {
this.documents.push(responseData.document);
}
);
}