简体   繁体   English

如何构建Meteor应用程序并将其加载到Meteor Shell中

[英]How to structure Meteor app and load into Meteor shell

I'm having a number of issues putting together a very simple piece of code as I learn Meteor. 在学习Meteor时,我遇到了很多问题,它们构成了非常简单的代码。 See the comments, which are questions. 请参阅注释,这些都是问题。

server/main.js 服务器/ main.js

import { Meteor } from 'meteor/meteor';

import { Post } from './schema'
// Why is this required to make Post available in Meteor.startup?
// Isn't there auto-loading?

Meteor.startup(() => {
    console.log(Post)
    // This works, but why isn't Post available to meteor shell?
});

server/schema.js 服务器/schema.js

import { Post } from './models/post'
export { Post }

server/models/post.js 服务器/模型/ post.js

import { Class } from 'meteor/jagi:astronomy';
// Why can't this be imported elsewhere, like main.js?

const Posts = new Mongo.Collection('posts');
const Post = Class.create({
  name: 'Post',
  collection: Posts,
  fields: {
    title: { type: String },
    userId: String,
    publishedAt: Date
  },
});

export { Post }

In addition to these questions, how can I load my app into meteor shell? 除了这些问题,如何将我的应用程序加载到流星外壳中? Post is undefined there, even though it's defined in Meteor.startup . Post是不确定的存在,即使在已定义Meteor.startup I tried using .load with an absolute path, but this breaks my app's imports, which use relative paths. 我尝试将.load与绝对路径一起使用,但这会中断我的应用程序的导入,该导入使用相对路径。

As for which errors I'm confused about: 至于我对哪些错误感到困惑:

  • When I try and use import inside Meteor.startup() , I get an error that the keyword import is undefined. 当我尝试在Meteor.startup()使用import ,出现一个错误,指出关键字import未定义。 I'm using the ecmascript package. 我正在使用ecmascript包。
  • When I don't import { Class } in the same file where I use Class , I get an unknown keyword error. 当我没有在使用Class的同一文件中import { Class }时,出现未知的关键字错误。
  • If I don't import { Post } in main.js, then Post is undefined. 如果我没有在main.js中import { Post } ,则Post是未定义的。
  • Not able to load app into Meteor shell. 无法将应用程序加载到Meteor Shell中。

To access exported objects in Meteor shell, use require : 要访问流星外壳中的导出对象,请使用require

> require('server/schema.js').Posts.findOne();

To access objects exported by packages, use the package name: 要访问包导出的对象,请使用包名称:

> require('react').PropTypes;

The reason you can't access an object that's imported by another js file is that each file has its own scope here. 您无法访问由另一个js文件导入的对象的原因是,每个文件在这里都有自己的作用域。 When Meteor builds your app, it doesn't just concatenate js files like many other build systems do, and that's really a good thing. 当Meteor构建您的应用程序时,它不仅像许多其他构建系统一样连接js文件,这确实是一件好事。

Basically a Javascript object gets created for every js file you write. 基本上,将为您编写的每个js文件创建一个Javascript对象。 Anything you export in a js file becomes a field in this object which you can access by using require . 您在js文件中导出的所有内容都将成为此对象中的字段,您可以使用require访问。 And import is just a nicer version of the same thing. import只是同一件事的一个更好的版本。

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

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