1

不用说,我对流星很陌生。我想将数据保存在我的流星 mongo 和 mini mongo(在浏览器上)并维持 maleCount 和 femaleCount 的值。

几年后我正在编写代码。它将从以下显示。请帮助我将“maleCount”和“femaleCount”的值存储到本地“post”集合中,将它们检索到我的会话中并在运行时操作它们。谢谢。

Javascript代码 -

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;
          }
});

post.find('1').fetch() 之类的查询;有时会向我返回错误,当我将其值存储在对象中时,返回未定义。谁能指出我正确的方向?

4

0 回答 0