2016-09-01 2 views
-1

Итак, если я иду в базу данных через meteor mongo, а когда я иду, то тип db.Questions.find ({}). Count(); он возвращает значение вопросов, которые у меня есть ведьма верна, но по какой-то причине мой код не работает, как я могу это исправить.Meteor Collection не загружается

database.js

import { Mongo } from 'meteor/mongo'; 

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

router.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'; 
}); 

ответ

0

Если бы мне пришлось Quess, ваша проблема, вероятно, связано с (не) публикации/подписки. 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'); 
}); 
Смежные вопросы