使用meteor从网页获取数据并将其设置为mongo集合

时间:2015-06-24 07:23:32

标签: mongodb meteor minimongo

毋庸置疑,我对流星很新。我想在我的meteor 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();有时会向我返回错误,当我将其值存储在对象中时,返回undefined。有人能指出我正确的方向吗?

0 个答案:

没有答案