1
我正嘗試使用Meteor.js創建多路徑Web應用程序。我添加了iron:router軟件包,並在「lib」文件夾(根目錄)中創建了以下router.js文件。它看起來像這樣:Meteor.js:ReferenceError:未定義窗口
//////////////////////////
// Router
//////////////////////////
Router = {
uri: _.compact(window.location.pathname.split("/")),
routes: [],
addRoute: function(route, template) {
var segments = _.compact(route.split("/"));
var placeholders = _.reduce(segments, function(currentArr, piece, index) {
if (piece.substr(0, 1) === ":") {
currentArr.push(index);
segments[index] = piece.substr(1);
}
return currentArr;
}, []);
this.routes.push({
route: segments,
template: template,
placeholderIndexes: placeholders
});
},
getMatchingRoute: function(){
for (var i in this.routes) {
var route = this.routes[i];
var data = {};
if (route.segments.length === this.uri.length) {
var match = _.every(route.segments, function(seg, i){
if (_.contains(route.placeholderIndexes, i)) {
data[seg] = this.uri[i];
return true;
} else {
return seg === this.uri[i];
}
}, this);
if (match) {
return {
data: data,
template: route.template
}
}
}
}
//no matches (add 404 or default template maybe?)
return false;
},
run: function(){
var route = this.getMatchingRoute();
if (route) {
var fragment = Meteor.render(function() {
if (Template[route.template] !== undefined) {
return Template[route.template](route.data);
}
});
document.body.appendChild(fragment);
} else {
//404
}
}
};
當本地流星服務器上運行,我得到了以下的輸出:
=> Started proxy.
=> Started MongoDB.
W20160211-10:47:37.487(-8)? (STDERR)
W20160211-10:47:37.487(-8)? (STDERR) /Users/***************/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20160211-10:47:37.488(-8)? (STDERR) throw(ex);
W20160211-10:47:37.488(-8)? (STDERR) ^
W20160211-10:47:37.488(-8)? (STDERR) ReferenceError: window is not defined
W20160211-10:47:37.488(-8)? (STDERR) at lib/router.js:6:20
W20160211-10:47:37.488(-8)? (STDERR) at /Users/************/www/myapp/.meteor/local/build/programs/server/app/lib/router.js:77:4
W20160211-10:47:37.488(-8)? (STDERR) at /Users/************/www/myapp/.meteor/local/build/programs/server/boot.js:242:10
W20160211-10:47:37.488(-8)? (STDERR) at Array.forEach (native)
W20160211-10:47:37.489(-8)? (STDERR) at Function._.each._.forEach (/Users/************/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20160211-10:47:37.489(-8)? (STDERR) at /Users/*************/www/myapp/.meteor/local/build/programs/server/boot.js:137:5
=> Exited with code: 8
W20160211-10:47:41.062(-8)? (STDERR)
W20160211-10:47:41.062(-8)? (STDERR) /Users/*************/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20160211-10:47:41.063(-8)? (STDERR) throw(ex);
W20160211-10:47:41.063(-8)? (STDERR) ^
W20160211-10:47:41.069(-8)? (STDERR) ReferenceError: window is not defined
W20160211-10:47:41.069(-8)? (STDERR) at lib/router.js:6:20
W20160211-10:47:41.069(-8)? (STDERR) at /Users/************/www/myapp/.meteor/local/build/programs/server/app/lib/router.js:77:4
W20160211-10:47:41.069(-8)? (STDERR) at /Users/*************/www/myapp/.meteor/local/build/programs/server/boot.js:242:10
W20160211-10:47:41.070(-8)? (STDERR) at Array.forEach (native)
W20160211-10:47:41.070(-8)? (STDERR) at Function._.each._.forEach (/Users/*************/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20160211-10:47:41.070(-8)? (STDERR) at /Users/**************/www/myapp/.meteor/local/build/programs/server/boot.js:137:5
=> Exited with code: 8
W20160211-10:47:44.633(-8)? (STDERR)
W20160211-10:47:44.634(-8)? (STDERR) /Users/**************/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20160211-10:47:44.634(-8)? (STDERR) throw(ex);
W20160211-10:47:44.634(-8)? (STDERR) ^
W20160211-10:47:44.640(-8)? (STDERR) ReferenceError: window is not defined
W20160211-10:47:44.640(-8)? (STDERR) at lib/router.js:6:20
W20160211-10:47:44.641(-8)? (STDERR) at /Users/***************/www/myapp/.meteor/local/build/programs/server/app/lib/router.js:77:4
W20160211-10:47:44.641(-8)? (STDERR) at /Users/***************/www/myapp/.meteor/local/build/programs/server/boot.js:242:10
W20160211-10:47:44.641(-8)? (STDERR) at Array.forEach (native)
W20160211-10:47:44.641(-8)? (STDERR) at Function._.each._.forEach (/Users/***************/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20160211-10:47:44.641(-8)? (STDERR) at /Users/**************/www/myapp/.meteor/local/build/programs/server/boot.js:137:5
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.
這是怎麼回事?爲什麼它不識別窗口變量。
聽起來就像你試圖在node.js下運行瀏覽器端代碼 – Quentin