简体   繁体   English

流星:全局常量未从app / lib / _constants.js中获取

[英]Meteor: Global constant not getting picked up from app/lib/_constants.js

My app directory structure is: 我的应用程序目录结构为:

App
├── client
├── lib
│   ├── _constants.js
│   ├── config
│   └── router
├── modules
│   ├── answers
│   └── questions
├── node_modules
│   └── bcrypt
├── public
│   └── imgs
├── server
│   ├── lib
│   ├── roles
│   └── startup
└── settings-example.json

In my _constants.js , I have defined some global variables, eg Schemas = {} which I intend to use in the modules > module_name> lib > collections.js or modules > module_name> lib > methods.js _constants.js ,我定义了一些全局变量,例如Schemas = {} ,打算在modules > module_name> lib > collections.jsmodules > module_name> lib > methods.js

But the global variables are not found in the modules' collections.js. 但是在模块的collections.js中找不到全局变量。 Here's the error I get: 这是我得到的错误:

W20160323-21:38:58.977(-7)? (STDERR) ReferenceError: Schemas is not defined
W20160323-21:38:58.977(-7)? (STDERR)     at modules/answers/lib/collections.js:22:1
W20160323-21:38:58.977(-7)? (STDERR)     at modules/answers/lib/collections.js:89:1

By my understanding, the global variables in the APP/lib/_constants.js file should have been loaded before the deeper modules/module_name/lib/collections.js got loaded, right? 据我了解, APP/lib/_constants.js文件中的全局变量应该在加载更深的modules/module_name/lib/collections.js APP/lib/_constants.js之前加载,对吗?

But that's obviously not happening. 但这显然没有发生。 What am I doing wrong? 我究竟做错了什么?

Thanks for your help! 谢谢你的帮助!

Read the "file load order" section from Structuring your application : 阅读“ 构建应用程序 ”中的“文件加载顺序”部分:

There are several load ordering rules. 有几个负载排序规则。 They are applied sequentially to all applicable files in the application, in the priority given below: 它们按以下优先级顺序应用于应用程序中所有适用的文件:

  1. HTML template files are always loaded before everything else HTML模板文件总是先加载
  2. Files beginning with main. 以main开头的文件。 are loaded last 最后加载
  3. Files inside any lib/ directory are loaded next 接下来将加载任何lib /目录中的文件
  4. Files with deeper paths are loaded next 接下来加载路径更深的文件
  5. Files are then loaded in alphabetical order of the entire path 然后按照整个路径的字母顺序加载文件

The way this is implemented, a deeply nested lib is loaded before a less deeply nested lib , which explains your problem. 实现此方法的方式是,在嵌套lib 之前先加载嵌套较深的lib ,这可以说明您的问题。 Here are some options: 以下是一些选项:

  1. Don't use lib in your deep paths. 不要在深层路径中使用lib Eg rename the path like modules/questions/stuff/collections.js . 例如,将路径重命名为modules/questions/stuff/collections.js
  2. Move your modules into packages. 将模块移动到软件包中。
  3. Upgrade to meteor 1.3 (still pre-release as of this writing) and start using the explicit export / import module syntax. 升级到流星1.3(在撰写本文时仍为预发布),并开始使用显式的export / import模块语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM