gpt4 book ai didi

node.js - Mongoose - 插入两个集合 : one is ID dependant of the other

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:24 25 4
gpt4 key购买 nike

我有一个要插入到数据库中的对象数组:

        tracks = [new Track({name : "myname1"}), new Track({name : "myname2"})]

Track.collection.insert(tracks, {}, function(err, tracks){
console.log('err', err);
}

但即使数组中有 2 个对象,insert() 方法也会抛出错误:

[RangeError: Maximum call stack size exceeded]

(“tracks”文档已创建,但其中没有任何内容。)

编辑 Mongoose - RangeError: Maximum Call Stack Size Exceeded解决了第一个问题,但没有解决我真正的问题:

当您从模型创建对象时,您可以获得 _id,我用它来创建其他对象。但不可能插入 Mongoose 对象的集合,因为已解决的问题:您必须仅使用数组 JSON 对象

data.items.forEach(function(item,index){
tracks.push(new Track({name : "myname1"}) );
posts.push(new Post({trackId : track._id}) );
}

Track.collection.insert(tracks, {}, function(err, tracks){
console.log('err', err);
}

最佳答案

我发现的解决方案是创建自己的唯一 ID,而不是获取 mongoose 对象的 ID ...并使用 JSON 对象:

data.items.forEach(function(item,index){
tracks.push({name : "myname1"}) ); //name is unique
posts.push({trackId : track.name) );
}

Track.create(tracks, function(err){
console.log('err', err);
}

关于node.js - Mongoose - 插入两个集合 : one is ID dependant of the other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410878/

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