Angular+Firebase项目中Type 'FileUpload[][]'类型不匹配问题求助
Hey there! Let's break down this error you're seeing in your Angular + Firebase app and figure out how to fix it.
What's Causing This Error?
The core issue here is a type mismatch between what you're assigning and what TypeScript expects:
AngularFireList<FileUpload[]>is a special type from AngularFire that represents a reference to a Firebase Realtime Database node. It comes with built-in methods likequery,update, andremovethat let you interact with the database directly.FileUpload[][]is just a plain JavaScript 2D array of yourFileUploadobjects—no database-specific methods attached (like the missingqueryproperty mentioned in the error).
You're likely accidentally assigning the raw array of fetched uploads to a variable that's declared as an AngularFireList type. For example, this is a common mistake in the service or component code:
// ❌ Wrong: Assigning a raw array to an AngularFireList variable private uploadsRef: AngularFireList<FileUpload[]>; constructor(private db: AngularFireDatabase) { // valueChanges() returns an Observable that emits FileUpload[][] this.db.list('/uploads').valueChanges().subscribe(uploads => { this.uploadsRef = uploads; // Boom! Type mismatch here }); }
How to Fix It
You need to separate your database reference (the AngularFireList) from the actual fetched data (the FileUpload[][] array). Here's how to adjust your code:
1. Update Your Upload Service (upload-file.service.ts)
Keep the AngularFireList as your database reference, and use a separate Observable to hold the fetched data:
import { AngularFireDatabase, AngularFireList } from '@angular/fire/database'; import { Observable } from 'rxjs'; import { FileUpload } from './file-upload.model'; // Adjust path as needed @Injectable({ providedIn: 'root' }) export class UploadFileService { // This is your Firebase database reference (AngularFireList type) private uploadsDbRef: AngularFireList<FileUpload>; // This Observable emits your actual upload data (adjust type to FileUpload[][] if needed) uploads$: Observable<FileUpload[]>; constructor(private db: AngularFireDatabase) { // Initialize the database reference first this.uploadsDbRef = db.list('/uploads'); // Fetch the data as an Observable (use snapshotChanges() if you need keys) this.uploads$ = this.uploadsDbRef.valueChanges(); } // Your existing upload logic stays intact pushFileToStorage(fileUpload: FileUpload) { // ... your upload code here } }
2. Adjust Your Components to Use the Observable
In both uploadingpage.component.ts and displaypage.component.ts, subscribe to the uploads$ Observable to get the raw data array, instead of trying to use the AngularFireList directly:
// Example in displaypage.component.ts import { Component, OnInit } from '@angular/core'; import { UploadFileService } from '../upload-file.service'; import { FileUpload } from '../file-upload.model'; @Component({ selector: 'app-displaypage', templateUrl: './displaypage.component.html', styleUrls: ['./displaypage.component.css'] }) export class DisplaypageComponent implements OnInit { // This holds the raw data array (adjust to FileUpload[][] if your structure requires it) fileUploads: FileUpload[]; constructor(private uploadService: UploadFileService) { } ngOnInit(): void { this.uploadService.uploads$.subscribe(uploads => { this.fileUploads = uploads; // If your data comes back as a 2D array, you can flatten it with uploads.flat() if needed }); } }
3. Double-Check Variable Types
Make sure any variable meant to hold display data is declared as FileUpload[] or FileUpload[][], not AngularFireList<...>. Reserve AngularFireList only for references to your Firebase database nodes.
Quick Recap
The error happens because you're mixing up a Firebase database reference (AngularFireList) with the raw data array it fetches. By separating these two into distinct variables, you'll fix the type mismatch and get rid of that terminal error.
内容的提问来源于stack exchange,提问作者Steve Doson

