属性“ push”在类型“ any []”上不存在

时间:2019-08-27 10:41:04

标签: javascript angular typescript firebase-realtime-database

当我编译角度代码时,编译器会向我显示“类型'any []'上不存在属性'push'”此错误。分别显示.service.ts和.component.ts代码。

Service.ts文件

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from '@angular/fire/firestore';
import { AngularFireDatabase} from 'angularfire2/database';

@Injectable({
  providedIn: 'root'
})
export class UserService {

  readonly userspath = "Development/users/users";
  collectionRef= "Development"
  ref = "Development"
  usersRef="users"

  public usersArray = [];

  constructor(public af: AngularFireDatabase) { }

  getUser(){

   return  this.af.database.ref(this.ref).child(this.usersRef)    

 }
}

component.ts文件

import { Component, OnInit } from '@angular/core';
import { UserService } from '../services/user.service';
import { Router } from '@angular/router';
import { User } from '../models/user';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-employee',
  templateUrl: './employee.component.html',
  styleUrls: ['./employee.component.css']
})
export class EmployeeComponent implements OnInit {

  public user = [];
  usersArray = [];
   constructor(
    private userservice:UserService,
    private router: Router
   ) { }

  title = "Employees";
  ngOnInit( ) {
    this.userservice.getUser().on('value',snapshot => {
     snapshot.forEach(data => {
      this.usersArray.push(data.val());
     })

  })
 }

VScode向我展示类型为“ any []”的属性“ push”不存在此错误

1 个答案:

答案 0 :(得分:0)

尝试一下

ngOnInit( ) {
    this.userservice.getUser().on('value',snapshot => {
     this.usersArray = [];
     snapshot.forEach((data:any) => {
      this.usersArray.push(data.val());
     })

  })