【问题标题】:Meteor Collection not loading流星收藏未加载
【发布时间】:2016-09-07 23:15:22
【问题描述】:

因此,如果我通过 meteor mongo 进入数据库,当我输入 db.Questions.find({}).count(); 时,它会返回值我有 1 个女巫的问题是正确的,但由于某种原因,我的代码无法正常工作,我该如何解决这个问题。

数据库.js

import { Mongo } from 'meteor/mongo';

Questions = new Mongo.Collection("Questions");

路由器.js

import '../imports/api/database.js';

FlowRouter.route('/', {
    action: function() {
        BlazeLayout.render('MainPage');

        console.log(Questions.find({}).count());

        if(Meteor.isClient) {
          Template.register.events({
            'submit form'(event, template) {
              event.preventDefault();

              const EmailConst = template.find('#email').value;
              const UsernameCost = template.find('#username').value;
              const PasswordConst = template.find('#password').value;

              Accounts.createUser({
                username: UsernameCost,
                email: EmailConst,
                password: PasswordConst
              });
            }
          });

          Template.login.events({
            'submit form'(event, template) {
              event.preventDefault();

              const UsernameCost = template.find('#login-username').value;
              const PasswordConst = template.find('#login-password').value;

              Meteor.loginWithPassword(UsernameCost, PasswordConst);
            }
          });

          Template.logout.events({
            'click #LogoutUser'(event) {
              event.preventDefault();
              Meteor.logout();
            }
          })
        };

        Template.createquestion.events({
          'submit form'(event, template) {
            event.preventDefault();

            const QuestionNewSub = template.find('#question-subject').value;
            const QuestionBody = template.find('#question-area').value;
            const QuestionId = Math.floor((Math.random() * 9999999999) + 1);
            const QuestionUsername = Meteor.user().username;
            const QuestionNewActive = true;
            const creationDate = new Date();

            Questions.insert({
              id: QuestionId,
              poster: QuestionUsername,
              subject: QuestionNewSub,
              content: QuestionBody,
              active: QuestionNewActive,
              createdAt: creationDate
            });

            console.log("Question posted!");

          }
        })

        Template.getquestions.helpers({

          results() {
            Questions.find({
              active: true
            }).count();
          },

        });

    }
});

FlowRouter.route('/social', {
    action: function() {
        console.log("Yeah! We are on the social route!");
    }
});

main.js

import { Meteor } from 'meteor/meteor';

Meteor.onConnection(function() {
  import '../imports/api/database.js';
});

【问题讨论】:

    标签: javascript node.js mongodb meteor


    【解决方案1】:

    如果我不得不质疑,您的问题可能与(不是)发布/订阅有关。 https://www.meteor.com/tutorials/blaze/publish-and-subscribe

    首先,在服务器上,您需要从您的Questions 集合中发布要公开给模板的项目。

    if (Meteor.isServer) {
      // This code only runs on the server
      Meteor.publish('questions', function questionsPublication() {
        return Questions.find();
      });
    }
    

    然后,在您的客户端代码中,您需要订阅该发布:

    Template.getquestions.onCreated(function() {
      Meteor.subscribe('questions');
    });
    

    【讨论】:

      猜你喜欢
      • 2014-10-22
      • 2013-05-23
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2015-04-19
      • 2017-06-02
      相关资源
      最近更新 更多