gpt4 book ai didi

javascript - ERROR TypeError : data. 切片不是函数

转载 作者:太空狗 更新时间:2023-10-29 17:47:44 25 4
gpt4 key购买 nike

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 响应并尝试通过 Angular Material 垫表将其显示在 UI 中。请找到附加的代码片段,让我知道我所做的代码有什么错误,或者我是否需要改变我的方法来实现这一目标

JSON 网络服务

{
"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."
},

服务.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);
}
}

表.组件.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;
})
}

}

错误快照: Error Snapshot

最佳答案

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

关于javascript - ERROR TypeError : data. 切片不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51304884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com