对于学校项目,我正在使用nodejs应用程序。 我需要提交一些随机口袋妖怪的位置。此刻我使用虚拟数据,但我已经收到错误。
我使用mongoose向mongodb提交最多10个地点,但收到以下错误:
RangeError: Maximum call stack size exceeded
数据对mongoose Schema有效。当我只提交一个位置(因为它拥有,不包含在数组中)时,也会显示错误。
{ _id: 5702bcc7698a36c82833a943,
name: 'Pikachu',
pid: 0,
lng: 5.298302246195419,
lat: 51.68774273818562 }.
我的代码如下所示:
var startDate = new Date();
if (!req.query.lat || !req.query.lng){
res.status(400).json("Your location has not been included");
}
var lat = parseFloat(req.query.lat);
var lng = parseFloat(req.query.lng);
location.find({}).exec(function(err, locations){
if (err){ return next(err); }
var filteredlocations = [];
locations.forEach(function(fetchedLocation) {
distance = calcDistance(fetchedLocation.lat, fetchedLocation.lng, lat, lng);
if (distance < 1000){
filteredlocations.push(fetchedLocation);
}
});
if (filteredlocations.length < 10) {
var toAdd = 1 - filteredlocations.length;
var newlocations = [];
for (var i = 0; i < toAdd; i++){
var randomlocation = randomLocation(lat, lng);
var newlocation = new location({
"lat": randomlocation.lat,
"lng": randomlocation.lng,
"pid": 0,
"name": "Pikachu",
});
newlocations.push(newlocation);
}
console.log(newlocations[0]);
location.collection.insert(newlocations[0], onInsert);
function onInsert(err, docs){
if (err){
console.log("err: " + err);
} else {
filteredlocations.push(docs);
res.json(filteredlocations);
}
}
}
});