不能使用Meteor-Blaze访问mongodb数据库

Can't access Mongo db using Meteor-Blaze

本文关键字:mongodb 数据库 访问 Meteor-Blaze 不能      更新时间:2023-09-26

在"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({});
  },
});

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

AFAIK meteor 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 || {});
   }
});

和一些允许再次,如果你没有流星帐户,你可以简单地-返回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
  });
});