【问题标题】:Meteor Collection RefrenceError流星集合RefrenceError
【发布时间】:2014-04-05 23:52:35
【问题描述】:

我正在运行 Meteor 0.8.0

尝试插入 Meteor 集合时,我从浏览器控制台收到 ReferenceError。仅供参考,我已删除此应用的自动发布包,以便使用发布和订阅。

模板

<head>
<title>itemsApp</title>
</head>

<body>
{{> name}}
{{> items}}
</body>

<template name="name">
<input type="text" />
</template>

<template name="items">
<h1>Items</h1>
<ul>
{{#each items}}
<li>{{name}} | {{category}}</li>
{{/each}}
</ul>
</template>

代码

var Items = new Meteor.Collection("items");

if (Meteor.isClient) {
Meteor.subscribe("items");

Template.items.items = function () {
return Items.find();
};
}

if (Meteor.isServer) {
Meteor.publish("items", function () {
return Items.find();
});
}

现在,从浏览器控制台(Ubuntu 13.10 上的 FF28 和 Chromium 33.0.1750.152),我得到了

ReferenceError: 项目未定义

当我跑步时:

Items.insert({name: "iPod", category : "Apple"});

有什么想法吗?

谢谢!

【问题讨论】:

    标签: javascript collections meteor referenceerror


    【解决方案1】:

    在 Meteor 中,使用 var 关键字定义的变量对于它们所在的文件是本地的。所以在你的情况下

    var Items = new Meteor.Collection("items");
    

    是本地的。只需删除关键字:

    Items = new Meteor.Collection("items");
    

    现在 Items 是一个全局变量,可以在其他文件(以及从控制台)中访问。

    【讨论】:

    • 感谢 Hubert OG,成功了!我认为在 Meteor 中提供某种范围封​​装非常有用,尽管这完全出乎意料。再次感谢好友。
    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多