本文介绍了错误TypeError:data.slice不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ERROR TypeError: data.slice is not a function
#atMatTableDataSource.push../node_modules/@angular/material/esm5/table.es5.js.MatTableDataSource._orderData (table.es5.js:742)

#at MapSubscriber.project (table.es5.js:675)
#atMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:35)
#atMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)

我正在使用JSON响应,并尝试通过有角度的材质垫表在UI中显示它.请找到随附的代码段,并让我知道我编写的代码中有什么错误,或者我需要更改方法来实现此目的

I am consuming a JSON response and attempting to display it in the UI through angular material mat-table. please find the attached snippet of code and let me know what is the mistake in the code which I have made or do I need to change my approach to achieve this

{
"data": [
{
  "action": "Ok",
  "created_user": "slstst5",
  "latest_instance": 7713997,
  "modified_dt": "Thu, 12 Jul 2018 06:27:32 GMT",
  "no_of_btl": 159,
  "request": 238244193,
  "sales_rep": "slstst5",
  "status_cd": "Submitted to Prov."
},

Service.ts

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {sot} from './sot.model';
import { Observable } from "rxjs";

@Injectable({
providedIn: 'root'
})
export class SotServiceService {
private serviceUrl ="service URL";

constructor(private http : HttpClient) { }
getuser():Observable<sot[]>{
  return this.http.get<sot[]>(this.serviceUrl);
}
}

table.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { Observable } from "rxjs";
import {MatSort, MatSortable, MatTableDataSource,PageEvent, MatPaginator} 
from '@angular/material';
import {SotServiceService} from '../sot-service.service';

@Component({
selector: 'app-sot-table',
templateUrl: './sot-table.component.html',
styleUrls: ['./sot-table.component.css']
})
export class SotTableComponent implements OnInit {
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator : MatPaginator;
dataSource;
//displayedColumns = ['id', 'name', 'username', 'email', 'phone', 'website'];
displayedColumns = ['request', 'status_cd', 'sales_rep', 'created_user', 
'modified_dt', 'action','no_of_btl'];
//displayedColumns=['userId','id','title','body']
constructor(private sotservice :SotServiceService) { }

ngOnInit() {
this.sotservice.getuser().subscribe(result => {

  if(!result){
    return ;
  }
  this.dataSource= new MatTableDataSource(result);
  this.dataSource.sort= this.sort;
  this.dataSource.paginator=this.paginator;
 })
 }

}

错误快照:

推荐答案

在错误的数据类型上使用slice函数时,有时会发生这种情况.切片适用于数组,而不适用于对象.您可能会尝试切片对象.

This happens sometimes when you are using the slice function on wrong data type. Slice works on an array, not an object. It might happen that you are trying to slice an object.

这篇关于错误TypeError:data.slice不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:21