我一直在关注2014年的基础教程,但绝对很棒。我什至无法超越第一部分。
在处理一些非常基本的导出时出现错误。首先,我将放下我的代码(多么令人讨厌)并逐步解决我的错误
package.json:
{
"name": "hoarserver",
"version": "0.0.1",
"private": true,
"dependencies": {
"minimist": "*",
"extend": "*",
"binary-parser": "*",
"mongoose": "*",
"underscore": "*"
}
}
server.js:
require(__dirname + '/Resources/config.js');
var fs = require('fs');
var net = require('net');
var i_files = fs.readdirSync(__dirname + "/Initializers");
i_files.forEach(function(file) {
console.log('Loading initializers ' + file);
require(__dirname + "/Initializers/" + file);
});
var model_files = fs.readdirSync(__dirname + "/Models");
model_files.forEach(function(file) {
console.log('Loading models ' + file);
require(__dirname + "/Models/" + file);
});
tiles = {};
var tile_files = fs.readdirSync(config.data_paths.tiles);
tile_files.forEach(function(file) {
console.log('Loading tile ' + file);
var tile = require(config.data_paths.tiles + file);
tiles[tile.tid] = tile;
});
console.log(config.database);
config.js:
var args = require('minimist')(process.argv.slice(2));
var extend = require('extend');
//Store the environment variable
var environment = args.env || "test";
//Common config... ie: name, version, max player etc...
var common_conf = {
name: "HOARServer",
version: "0.0.1",
environment: environment,
data_paths: {
tiles: __dirname + "\\Game Data\\" + "Tiles\\",
cosmetics: __dirname + "\\Game Data\\" + "Cosmetics\\",
objects: __dirname + "\\Game Data\\" + "Objects\\"
}
};
var conf = {
production: {
ip: args.ip || "0.0.0.0",
port: args.port || 8081,
database: "mongodb://127.0.0.1/hoar_p"
},
test: {
ip: args.ip || "0.0.0.0",
port: args.port || 8082,
database: "mongodb://127.0.0.1/hoar_t"
}
};
extend(false, conf.production, common_conf);
extend(false, conf.test, common_conf);
module.exports = config = conf[environment];
当Webstorm建议config
作为元素尚未导出时,会出现问题,尽管在导出时将其设置为conf[environment]
时(被偷偷地定义了)。
运行代码时,配置运行server.js,环境设置为test
;
我漂亮的控制台输出不仅告诉我data_paths是未定义的,而且还告诉我配置ITSELF也是未定义的! :)
var tile_files = fs.readdirSync(config.data_paths.tiles);
^
TypeError: Cannot read property 'data_paths' of undefined
at Object.<anonymous> (C:\Users\autum\WebstormProjects\HOARServer\server.js:17:40)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
Process finished with exit code 1
我有:
有帮助吗? :)