angulartypescriptfirebaseangularfirespread-syntax

breaks a service during migration to Angular 9


I usually use this method to get my values ​​from firebase. I have no problem with getUsers ().

But since the switch to angularfire6, I have a problem with getUserById there: "... data"

"error: spread type may only created from object types"

import { Injectable } from "@angular/core";
import { User } from "../models/user.model";
import {
  AngularFireDatabase,
  AngularFireList,
  AngularFireObject,
} from "@angular/fire/database";
import { Observable } from "rxjs";
import { switchMap, map } from "rxjs/operators";
import * as firebase from "firebase";
import DataSnapshot = firebase.database.DataSnapshot;

@Injectable({ providedIn: "root" })
export class UsersService {
  users: AngularFireList<any>;

  constructor(private database: AngularFireDatabase) {
    this.users = database.list("users");
  }

  getUsers() {
    return this.users.snapshotChanges().pipe(
      map((actions) => {
        return actions.map((a) => {
          const data = a.payload.val();
          const key = a.payload.key;
          return { key, ...data };
        });
      })
    );
  }

  getUserById(UserId: string) {
    return this.database
      .list("/users", (ref) => ref.orderByChild("id").equalTo(UserId))
      .snapshotChanges()
      .pipe(
        map((actions) => {
          return actions.map((a) => {
            const data = a.payload.val();
            const key = a.payload.key;
            return { key, ...data };
          });
        })
      );
  }
}

No doubt is this the switch to "typescript": "3.8.3"? Thank you for your help


Solution

  • return this.database.list('/users', ref => 
    ref.orderByChild("id").equalTo(UserId)).snapshotChanges().pipe(map(actions => {
    return actions.map(a => {
    const data: Object  = a.payload.val();
    const key = a.payload.key;
    return {key, ...data} ;