流星.js:引用错误:窗口未定义
Meteor.js: ReferenceError: window is not defined
我正在尝试使用Meteor.js创建一个多路线网络应用程序。 我已经添加了 iron:router 包,并在"lib"文件夹(根目录)中创建了以下 router.js 文件。 它看起来像这样:
//////////////////////////
// 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
}
}
};
在本地 meteor 服务器上运行时,我得到以下输出:
=> 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.
这是怎么回事? 为什么它无法识别窗口变量。
lib
文件夹中的项目同时在服务器和客户端上运行。 由于服务器上不存在window
,因此会引发错误。
相关文章:
- 如何解决'窗口未定义'终端上的节点JS出错
- Internet窗口未定义
- requirejs优化器错误:未定义窗口
- window.location.href窗口未在javascript中定义
- $window错误'$窗口未定义'Angular应用程序中的谷歌分析代码
- 窗.WebSocket - 未定义窗口
- Google Maps API V3 信息窗口错误:“信息窗口未定义”
- 窗口未使用对等.js定义
- 为流星重新包装砌体会产生错误“引用错误:窗口未定义”
- Ace 编辑器:未捕获的引用错误:窗口未定义
- 谷歌地图 api v3 - “信息窗口未定义”错误
- 流星.js:引用错误:窗口未定义
- $=require('jQuery')导致'窗口未定义'.该怎么办
- AngularJS”;ReferenceError:窗口未定义“;在NodeJS应用程序中
- requirejs窗口未定义
- 用Gulp摩卡测试Angular:“;窗口未定义”;
- AWS Cognito未认证登录错误(窗口未定义)[JS]
- SystemJS Builder -窗口未定义
- 使用Grunt的Node JS项目,生成的JS文件有ReferenceError:窗口未定义
- 如何正确处理 React 服务器端渲染中的窗口=未定义错误