我使用单个脚本将虚拟数据插入到我的应用程序的数据库中。我是node.js的新手并且怀疑我可能正在吹堆栈内存......但是:
我正在获取流程退出代码3,根据文档:
3 Internal JavaScript Parse Error - The JavaScript source code internal in Node's bootstrapping process caused a parse error. This is extremely rare, and generally can only happen during development of Node itself.
我使用3个循环,它是所有嵌套的回调代码。由于我是JavaScript后端编程的新手,我不确定堆栈是否可能成为问题,但退出代码并不能让它听起来像堆栈问题。
以下是代码:
var mongoose = require('mongoose');
var UserModel = require('../models/UserModel');
UserModel.registerSchema(mongoose);
var TeamModel = require('../models/TeamModel');
TeamModel.registerSchema(mongoose);
var PlayerModel = require('../models/PlayerModel');
PlayerModel.registerSchema(mongoose);
var fs = require('fs');
var parsedJSON = JSON.parse(fs.readFileSync('../dummy_data/dummy_player_data', 'utf8'));
var system_db = mongoose.connect('mongodb://localhost:27017/local_dev_db');
var userz = UserModel.getNewUser(system_db).find({}, function (err, users) {
users.forEach(function (user, userIndex) {
var user_id = user._id.toString();
var teamz = TeamModel.getNewTeam(system_db, user_id).find({}, function (err, teams) {
teams.forEach(function (team, teamIndex) {
parsedJSON.forEach(function (player, playerIndex) {
var Player = PlayerModel.getNewPlayer(system_db, user_id);
var newPlayer = new Player({
team_id: team._id,
firstName: player.firstName,
lastName: player.lastName,
hidden: player.hidden,
email: player.email,
phone: player.phone,
gender: player.gender,
age: player.age,
shirtNumber: player.shirtNumber
});
newPlayer.save(function (err, result) {
if (err) {
console.log("error in player save method:", err);
}
if (result) {
//console.log('Added!', result);
}
});
});
});
});
});
});
我在使用大量数据时只收到错误代码,这就是让我觉得它是堆栈问题的原因。我正在使用Mongoose进行MongoDB。关于可能发生什么的任何想法?
在getNewUser,getNewTeam,getNewPlayer里面我有:
exports.getNewUser = function (system_db) {
return system_db.model('users', userSchema);;
};
exports.getNewTeam = function (system_db,user_id) {
return system_db.model('teams_'.concat(user_id.toString()), teamSchema);
};
exports.getNewPlayer = function (system_db,user_id) {
return system_db.model('players_'.concat(user_id.toString()), playerSchema);
};
这些函数正在为每个用户,团队和玩家返回mongoose模型。