【问题标题】:Can't access Mongo db using Meteor-Blaze无法使用 Meteor-Blaze 访问 Mongo db
【发布时间】:2017-03-17 11:22:47
【问题描述】:

在“reports.html”的同一文件夹中的文件“reports.js”中使用此代码,我在返回的数组中得到 0 个元素。不确定我是否遗漏了任何额外的声明,但我也不能使用“import { Mongo } from 'meteor/mongo';”没有错误“使用保留字'import'”。 流星版本 1.2.1

JS

Tasks = new Mongo.Collection('tasks');

if(Meteor.isClient){
    Template.reports.rendered = function(){
        if(Session.get("animateChild")){

            $(".reports-page").addClass("ng-enter");

            setTimeout(function(){
                $(".reports-page").addClass("ng-enter-active");
            }, 300);

            setTimeout(function(){
                $(".reports-page").removeClass("ng-enter");
                $(".reports-page").removeClass("ng-enter-active");
            }, 600);
        }
    };
}

Template.dashboardd.helpers({
  options() {
    return Tasks.find({});
  },
});

HTML

<Template name="dashboardd">
  {{#each options}}
    <p><label>{{text}}</label></p>
  {{/each}}
</Template>

【问题讨论】:

  • 我认为您需要包含 babel 或 ecmascript 包才能使导入工作。您是否发布并订阅了任务集合?

标签: javascript html mongodb meteor meteor-blaze


【解决方案1】:

AFAIK 流星 1.2 不支持导入。进口是在流星 1.3 或 1.4 中添加的新模块,我不确定。 在处理集合时,我总是使用这 3 个元素(如果我们采用集合声明,则为 4 个)

您应该在服务器端进行一些发布并允许:

Meteor.publish("Tasks ", function (selector, options) {
//you can remove condition if you dont have accounts-package
   if (this.userId) {
    return Tasks .find(selector || {}, options || {});
   }
});

还有一些允许 再一次,如果您没有 Meteor 帐户,您可以简单地 - 在插入、更新等时返回 true。但我不必说这不安全:P

Tasks.allow({
 insert: function (userId, doc) {
return userId;
},

 update: function (userId, doc) {
    return userId;
 },

 remove: function (userId) {
  return userId;
 },
});

在客户端一些订阅

Template.YourTemplateName.onCreated(function () {
 this.autorun(() => {

    this.subscribe('Tasks', { _id: Template.currentData()._id });
 //or
   this.subscribe('Tasks');
 //but this solution can kill Browser when you collection will have a thousand elements. Allways try to not subscribe all collection. Only Needed fields/objects
  });
});

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 2015-06-18
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 2017-07-02
    相关资源
    最近更新 更多