我正在使用Angular2和Meteor。由于我在客户端添加了该代码,因此我的Chrome控制台出现了一个异常(超出了最大调用堆栈大小):
var userChannelsId = UserChannels.find({idUser: Meteor.userId()});
this.channels = Channels.find({_id: {$in: userChannelsId}});
根据我的发现,当存在无限循环时会发生此异常。我确定错误来自此行(在确定之前,我调试了代码)。
问题是否出在第二次开始时第一次搜索未完成的事实?
全部功能:
getChannelList(): void {
console.log('ok');
var userChannelsId = UserChannels.find({idUser: Meteor.userId()});
this.channels = Channels.find({_id: {$in: userChannelsId}});
console.log('ko');
this.channels.subscribe(channel => {
this.selectChannel(channel[0]);
});
}
编辑:
作为@MichaelSolati,我尝试仅获取userChannel的IdChannel,但我仍然遇到相同的错误……它返回一个可观察的数组,而不是数组。也许是问题所在?这是我所做的:
getChannelList(): void {
console.log('ok');
let userId = Meteor.userId();
var userChannelsId = UserChannels.find({idUser: userId});
var values = userChannelsId.map((userChannels:Array<UserChannel>) => {
let result:Array<String> = [];
if (userChannels) {
userChannels.forEach((userChan) => {
result.push(userChan.idChannel);
});
}
return result;
});
this.channels = Channels.find({_id: {$in: values}});
console.log('ko');
this.channels.subscribe(channel => { this.selectChannel(channel[0]); });
}
最佳答案
由于对查询使用MeteorRxJS而进行了更新。尽管这可能并不完美,但它应该使您走上正确的道路。
private userChannelsSub: Subscription;
getChannelList(): void {
console.log('ok');
let userId = Meteor.userId():
let this.userChannelsSub = UserChannels.find({idUser: userId}).subscribe((userChannels: any[]) => {
if (Array.isArray(userChannels)) {
let userChannelsId = userChannels.map((channel: any) => { return channel._id; });
if (this.channels) { this.channels.unsubscribe(); }
this.channels = Channels.find({_id: {$in: userChannelsId}}).subscribe((channels: any[]) => {
this.selectChannel(channels[0]);
});
}
});
console.log('ko');
}