Javascript 2 Collection.find超过了最大调用堆栈大小

Javascript 2 Collection.find超过了最大调用堆栈大小,javascript,mongodb,angular,meteor,Javascript,Mongodb,Angular,Meteor,我用的是Angular2和Meteor。我在Chrome控制台中有一个异常(超过了最大调用堆栈大小),因为我在客户端添加了该代码: var userChannelsId = UserChannels.find({idUser: Meteor.userId()}); this.channels = Channels.find({_id: {$in: userChannelsId}}); 从我发现的情况来看,当存在无限循环时,就会发生这种异常。我确信错误来自这几行(我调试了我的代码,直到我确定)。

我用的是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]); });
}
getChannelList():void{
console.log('ok');
让userId=Meteor.userId();
var userChannelsId=UserChannels.find({idUser:userId});
var values=userChannelsId.map((userChannels:Array)=>{
让结果:数组=[];
如果(用户通道){
userChannels.forEach((userChan)=>{
结果推送(userChan.idChannel);
});
}
返回结果;
});
this.channels=channels.find({u id:{$in:values}});
console.log('ko');
this.channels.subscribe(channel=>{this.selectChannel(channel[0]);});
}

由于使用MeteorXJS进行查询而更新。虽然它可能并不完美,但它可能会让你走上正确的道路

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');
} 

由于使用MeteorXJS进行查询而更新。虽然它可能并不完美,但它可能会让你走上正确的道路

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');
} 
好了,伙计们

我找到了一个非常有效的解决方案! 如果您对我的提议有任何改进意见,请随时填写评论,我将不胜感激:)

getChannelList():void{
让userId=Meteor.userId();
UserChannels.find({idUser:userId}).subscribe((userChannel:userChannel[])=>{
var channelsId:Array=[];
userChannel.forEach((userChan)=>{
channelsId.push(userChan.idChannel);
});
this.channels=channels.find({u id:{$in:channelsId}});
});
if(this.channels!==未定义){
this.channels.subscribe(频道=>{
选择频道(频道[0]);
});
}
}
谢谢大家的回答。

好了,伙计们

我找到了一个非常有效的解决方案! 如果您对我的提议有任何改进意见,请随时填写评论,我将不胜感激:)

getChannelList():void{
让userId=Meteor.userId();
UserChannels.find({idUser:userId}).subscribe((userChannel:userChannel[])=>{
var channelsId:Array=[];
userChannel.forEach((userChan)=>{
channelsId.push(userChan.idChannel);
});
this.channels=channels.find({u id:{$in:channelsId}});
});
if(this.channels!==未定义){
this.channels.subscribe(频道=>{
选择频道(频道[0]);
});
}
}


谢谢大家的回答。

使用
setTimeout(function(){//your code here},0})
避免调用堆栈大小超过问题我必须在函数中添加什么?所有我的getChannelList代码,或者只是我的Collection.find?您必须将无限代码放入
setTimeout
回调函数。问题是异常触发了超时,而我的代码没有执行。。。因此,我认为setTimeout只是避免了这个问题,它并没有解决这个问题:(最佳实践是不要订阅helper。helper会被反复调用,可能是以您不期望的方式调用,因此其中的任何操作都应该是幂等的。我通常会在onCreated()函数中处理所有订阅。使用
setTimeout(function()){//您的代码在这里},0})
为避免调用堆栈大小超出问题,我必须在函数中放入什么?我的所有getChannelList代码,或者只是我的Collection.find?您必须将无限代码放入
setTimeout
回调函数问题是异常触发超时,而我的代码没有执行…因此我认为setTimeout只是avoid问题,它无法解决:(最佳做法是不订阅帮助程序。帮助程序会被反复调用,可能是以您不期望的方式调用,因此其中的任何操作都应该是幂等的。我通常会在onCreated()中处理我的所有订阅)功能。我按照你的建议做了,但不幸的是,例外情况仍然存在:(等等……您正在尝试获取用户所属频道的列表并搜索所有频道,对吗?但您正在将
channels
find query传递给您的
UserChannels
查询的完整对象数组,而不是id数组。请在进行
UserChannels
query…
userChannelsId后尝试此操作。)=userChannelsId.map((channel)=>{return channel.\u id;});
我编辑了我的帖子,向您展示了我是如何尝试的。由于查询返回了一个UserChannel数组,我不得不对您的建议进行一些修改。请告诉我是否我做错了什么。(它仍然不起作用:()我突然想到你正在使用Meteorxjs……好吧……这会有点痛苦……我会更新我的答案。PS肯定有比我的解决方案更好的方法,但我与Meteor世界有点/非常疏远,我只是对我记忆中的东西嗤之以鼻(已经8个月了).我按照你的建议做了,但不幸的是,例外情况仍然存在:(等等……您正在尝试获取用户所属频道的列表并搜索所有频道,对吗?但您正在将
channels
find query传递给您的
UserChannels
查询的完整对象数组,而不是id数组。请在进行
UserChannels
query…
userChannelsId后尝试此操作。)=userChannelsId.map((channel)=>{return channel.\u id;});
我编辑了我的帖子,向您展示了我是如何尝试的。由于查询返回了一个UserChannel.Te数组,我不得不对您的建议做了一些修改