Getting and setting data from a webpage to a mongo collection using meteor

99 Views Asked by At

Needless to say, I am very new to meteor. I want to save data on my meteor mongo and mini mongo (on the browser) and sustain the values of maleCount and femaleCount.

I am writing code after a few years. It will show from the following. Please help me out in storing the value of my 'maleCount' and 'femaleCount' into the local 'post' collection , retrieve them into my session and manipulate them at runtime. Thanks.

Javascript code -

counter1 = 0;
counter2 = 0;
c1=0;
c2=0;
maleCount = 0;
femaleCount = 0;
// rec = post.find('1').fetch();
//     console.log(rec[0].maleCount);
//     //console.log(rec[1].maleCount);
//     maleCount = rec[0].maleCount;
//     femaleCount = rec[0].femaleCount;

clk = 0
//maleCount = cnt[0].maleCount;
//femaleCount = cnt[0].femaleCount;

if (Meteor.isClient) {
  //getCount();
  Session.setDefault('counter1', 0);
  Session.setDefault('counter2', 0);
  Session.set('maleCount', maleCount);
  Session.set('femaleCount', femaleCount);
    rec = post.find('1').fetch();
    console.log(rec[0].maleCount);
    //console.log(rec[1].maleCount);
    maleCount = rec[0].maleCount;
    femaleCount = rec[0].femaleCount;
    console.log(rec[0].femaleCount);
  Template.hello.helpers({
counter1: function () {
  c1 = Session.get('counter1');

  return c1;

console.log(c1);


},
counter2: function () {

  c2 = Session.get('counter2');
  return c2;

},

maleCount: function() {
mc = Session.get('maleCount'); 
return  mc;
},

femaleCount: function() {
fc = Session.get('femaleCount');
return fc;
},

post: function() {
  return post;

}
});

Template.hello.events({
'click #maleb': function () {

  // increment the counter when button is clicked
  Session.set('counter1', Session.get('counter1') + 1);
  clk++;
  console.log(clk);
  post.update({_id:"1"} , {$set: { 'maleCount' : clk } });

},

'click #femaleb': function () {
  // increment the counter when button is clicked
  Session.set('counter2', Session.get('counter2') + 1);
},

'click #malec': function () {
  // increment the counter when button is clicked
  Session.set('counter1', Session.get('counter1') - 1);

},

'click #femalec': function () {
  // increment the counter when button is clicked
  Session.set('counter2', Session.get('counter2') - 1);
},

'mouseleave #maleb' : function() {
Session.set('maleCount', maleCount);
post.update({_id:"1"} , {$set: {'maleCount' : clk}});
//Session.set('maleCount' , clk);
},

'mouseleave #femaleb' : function() {
Session.set('femaleCount', fc);
},

'mouseleave #malec' : function() {
Session.set('maleCount', mc);
},

'mouseleave #femalec' : function() {
Session.set('femaleCount', fc);
}





});
}

if (Meteor.isServer) {
  Meteor.startup(function () {

  });
}

common.js -

post = new Mongo.Collection("posts");
post.collection.validate(true);
Schema={};
function getCount()
{
rec = post.find('1').fetch();
console.log(rec[0].maleCount);    //console.log(rec[1].maleCount);
maleCount = rec[0].maleCount;
femaleCount = rec[0].femaleCount;


};
Schema.post = new SimpleSchema({_id:{type:String},
                       maleCount:       {type: Number,    label: "Title",        max: 200  , min :0},
                       femaleCount:       {type: Number,    label: "Title",        max: 200 , min:0}
                       }
                       );
post.attachSchema(Schema.post);
// rec = post.find('1').fetch();
// console.log(rec[0].maleCount);    //console.log(rec[1].maleCount);
// maleCount = rec[0].maleCount;
// femaleCount = rec[0].femaleCount;
//getCount();
//maleCount = this.maleCount;
//femaleCount = this.femaleCount;

if(post.find().count() === 0)
{
    post.insert({_id: "0" , 'maleCount' : 0 , 'femaleCount' : 0});
    //post.update({_id:"1"}, {$set: {'_id':'1', 'maleCount' : 'maleCount' , 'femaleCount' : 'femaleCount'}} , {upsert:true});
    //post.insert({_id:"1" , 'maleCount' : 'maleCount' , 'femaleCount' : 'femaleCount'});
    post.update({_id:"1"}, {$set : {'_id':'1','maleCount ' : 'maleCount' , 'femaleCount' : 'femaleCount'} } , {upsert : true});
}
//cnt = post.find('1');


//isValid = Match.test(cnt, Schema.post);


post.allow({
  insert: function(doc){
            return doc;
          },
  update: function(doc){
            return doc;
          },
  remove:  function(doc){
            return doc;
          }
});

queries like post.find('1').fetch() ; sometimes return error to me and when I store its value in an object, returns undefined. Can anyone point me in the right direction please?

0

There are 0 best solutions below