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

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

我的应用程序目录结构是:

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

在我的 _constants.js 中,我定义了一些全局变量,例如Schemas = {} 我打算在 modules > module_name> lib > collections.jsmodules > module_name> lib > methods.js

中使用

但是在模块 collections.js 中找不到全局变量。这是我得到的错误:

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

根据我的理解,APP/lib/_constants.js文件中的全局变量应该在更深的modules/module_name/lib/collections.js加载之前加载,对吧?

但这显然不会发生。我究竟做错了什么?

感谢您的帮助!

阅读 Structuring your application 中的 "file load order" 部分:

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
  2. Files beginning with main. are loaded last
  3. Files inside any lib/ directory are loaded next
  4. Files with deeper paths are loaded next
  5. Files are then loaded in alphabetical order of the entire path

这是实现的方式,在 之前 加载嵌套较浅的 lib,这解释了您的问题。以下是一些选项:

  1. 不要在你的深度路径中使用 lib。例如。将路径重命名为 modules/questions/stuff/collections.js.
  2. 将您的模块移动到包中。
  3. 升级到 meteor 1.3(在撰写本文时仍处于预发布状态)并开始使用显式 export/import 模块语法。