在AngularFire v7.6中,出现"FirebaseError: Expected first argument to collection() to be a CollectionReference"错误是因为在使用collection()
方法时,传递的第一个参数不是一个有效的CollectionReference
对象。
要解决这个错误,可以按照以下步骤进行修改:
AngularFireModule.initializeApp()
方法中,已经正确配置了Firebase的配置信息。例如:import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';
@NgModule({
imports: [
AngularFireModule.initializeApp(environment.firebaseConfig),
// ...
],
// ...
})
export class AppModule { }
import { AngularFirestore } from '@angular/fire/firestore';
@Injectable()
export class MyService {
constructor(private firestore: AngularFirestore) {
// ...
}
}
collection()
方法时,传递的第一个参数是一个有效的CollectionReference
对象。例如:import { AngularFirestore, CollectionReference } from '@angular/fire/firestore';
@Injectable()
export class MyService {
constructor(private firestore: AngularFirestore) {
// 获取一个有效的CollectionReference
const collectionRef: CollectionReference = this.firestore.collection('myCollection').ref;
// 使用collectionRef进行查询等操作
}
}
请注意,collection()
方法需要传递一个有效的CollectionReference
对象作为第一个参数。如果你传递的是一个字符串,例如collection('myCollection')
,它将会被认为是一个路径字符串,并不是一个有效的CollectionReference
对象。
希望这个解决方法对你有帮助!