2016-08-05 27 views

答えて

0

ちょうどinsertそれをコレクションにします。ここでupsert(存在する場合すなわち、更新し、ない場合は挿入)関数があります:

if (Saves.find({_id: Meteor.userId()})){ 
    Saves.update({_id: Meteor.userId()}, {save: save}) 
    console.log("Updated saves") 
} 
else { 
    Saves.insert(save) 
} 
0

autopublishパッケージが存在する場合、あなたは、単にMongo.Collectionを作成し、データベースにこのカウンタを挿入することができます。

var myCounter = 5; 
var collection = new Mongo.Collection('collection'); 
collection.insert({counter: myCounter}); 

お役に立てれば。

1

データを永続化するために、サーバー側のコレクションを作成します。

Meteor.isServer { 
    Counter= new Mongo.Collection('Counter'); 
    // Server side method to be called from client 
    Meteor.methods({ 
     'updateCounter': function (id) { 
      if(typeof id && id) { 
      return Counter.update({_id: id}, {$set: {counter: {$inc: 1}}}); 
      } else { 
      return Counter.insert({counter: 1}) 
      } 
     } 
    }) 
    // Publication 
    Meteor.publish("counter", function() { 
     Counter.find(); 
    }) 
} 

あなたは、クライアントでデータをサブスクライブすることができます。これにより

Meteor.isClient{ 
    Template.yourTemplateName.created = function() { 
     Meteor.subscribe('counter'); 
    } 
    Template.yourTemplateName.heplers(function() { 
     counter: function() { 
      return Counter.findOne(); 
     } 
    }) 
    Template.yourTemplateName.event(function() { 
     'click #counterButtonIdName': function() { 
      if(Counter.findOne()) { 
       Meteor.call('updateCounter', Counter.findOne()._id); 
      } else { 
      Meteor.call('updateCounter', null); 
      } 
     } 
    }) 
} 

のHTMLサンプル

<template name="yourTemplateName"> 
    <span>{{counter}}</span> //area where count is written 
</template> 

をあなたのデータの安全なサーバー側の処理を達成することができますし、あなたがデータを持つまではカウントは永続的になりますデータベース。また、この方法でMeteorの基本を学ぶことができます。

関連する問題