import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class FirestoreDbService {
constructor(private db: AngularFirestore) { }
getBuildList() {
// return this.db.collection('Builds').valueChanges();
return this.db.collection('Builds').snapshotChanges().pipe(
map(docArray => {
return docArray.map(doc => {
console.log('==', doc.payload.doc.id);
console.log('$==$', doc.payload.doc.data());
return{
id: doc.payload.doc.id,
... doc.payload.doc.data()
}
})
})
)
}
}
On the last line I am getting the error. I think this is something to do with typescript. Any help appreciated
The "Spread types may only be created from object types" error occurs when we try to use the spread operator with a value that is possibly not an object. To solve the error, use a conditional to make sure the value is an object before using the spread operator.
If we are using TypeScript version 4 or beyond, we can spread tuple generic parameter types. This is useful to help TypeScript better infer types on expressions that involve tuples.
Use the as
keyword:
... doc.payload.doc.data() as {}
This will tell the compiler to treat doc.payload.doc.data()
as an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With