当我尝试更改页面(小于)时,遵循 Angular 2 - Meteor分页说明给了我这个错误:pageSize
totalItems
Exception in queued task: EXCEPTION: Error in client/components/entities/players/player-list.html:2:8
ORIGINAL EXCEPTION: TypeError: Cannot read property 'type' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'type' of undefined
at AppElement.detachView (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:11331:17)
at ViewContainerRef_.remove (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:11531:34)
at NgFor._bulkRemove (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:21719:37)
at NgFor._applyChanges (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:21684:33)
at NgFor.ngDoCheck (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:21670:22)
at DebugAppView._View_PlayerList0.detectChangesInternal (PlayerList.template.js:94:41)
at DebugAppView.AppView.detectChanges (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:12703:14)
at DebugAppView.detectChanges (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:12808:44)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:12729:19)
at DebugAppView._View_PlayersPage0.detectChangesInternal (PlayersPage.template.js:395:8)
ERROR CONTEXT:
[object Object]
9debug.js:41 Exception in queued task: TypeError: Cannot read property 'splice' of null
at MongoCursorObserver._removeAt (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:77671:19)
at removedAt (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:77649:35)
at http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:77364:20
at ZoneDelegate.invoke (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:124251:29)
at Zone.run (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:124144:44)
at Object.removedAt (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:77363:23)
at removed (http://localhost:3000/packages/minimongo.js?hash=88217d643bc16fdf3505c6d4b2b8f5ddc400c49a:3745:28)
at self.applyChange.removed (http://localhost:3000/packages/minimongo.js?hash=88217d643bc16fdf3505c6d4b2b8f5ddc400c49a:3674:44)
at http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:77364:20
at ZoneDelegate.invoke (http://localhost:3000/packages/modules.js?hash=fa01f730b3659d348eabf8ba338dffb7d96b4033:124251:29)
我的文件是他们教程的简化版本:
import { Component } from '@angular/core';
import { Players } from '../../../../collections/players';
import { Mongo } from 'meteor/mongo';
import { MeteorComponent } from 'angular2-meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { PaginationService, PaginatePipe, PaginationControlsCmp } from 'angular2-pagination';
@Component({
selector: 'player-list',
viewProviders: [PaginationService],
templateUrl: 'client/components/entities/players/player-list.html',
directives: [PaginationControlsCmp],
pipes: [PaginatePipe]
})
export class PlayerList extends MeteorComponent{
players: Mongo.Cursor<Party>;
pageSize: number = 5;
curPage: ReactiveVar<number> = new ReactiveVar<number>(1);
nameOrder: number = 1;
constructor() {
super();
this.autorun(() => {
let options = {
limit: this.pageSize,
skip: (this.curPage.get() - 1) * this.pageSize,
sort: { name: this.nameOrder }
};
this.subscribe('players', options, () => {
this.players = Players.find({}, { sort: { name: this.nameOrder } });
}, true);
});
}
onPageChanged(page: number) {
this.curPage.set(page);
}
}
以及相关的 HTML:
<div>
<ul>
<li *ngFor="let player of players | paginate:{currentPage: 1, itemsPerPage: pageSize, totalItems: 14}">
<p>{{player.name}}</p>
</li>
</ul>
<pagination-controls (change)="onPageChanged($event.page)"></pagination-controls>
</div>
我已签入该publish
函数以验证每次返回的文档数量是否正确。(.fetch().length
用于正确衡量 的影响limit: 10
)
我试过 的我已经改变了:
this.players = Players.find({}, { sort: { name: this.nameOrder } });
至:
this.players = Players.find({}, { sort: { name: this.nameOrder } }).fetch();
这可以防止在更改分页页面时出现上述错误。
这会创建一个新的(非破坏性)错误,其中length
集合在切换页面后从 5 变为 10 - 再次切换页面似乎使其保持在 10。好像订阅正在记住上次订阅调用中的文档,即使publish
日志显示要返回的正确数量的文档。
问题
应该
ngFor
使用游标吗?如果是这样,什么可能导致错误发生?订阅是否有理由记住以前的文档?我尝试存储订阅并调用
stop()
,但这导致界面出现故障。