mongodb - Meteor: insert produces multiple entries -


my question following.

i'm starting working meteor , wrote small code against meerkat.

i'd insert users database. first have call meerkat username meerkatid , can user info.

this code.

users = new mongo.collection("users");      if (meteor.isclient) {    template.body.helpers({    users: function () {        // otherwise, return of tasks        return users.find({});    }    });    template.user.helpers({    parentusername: function (parentcontext) {          return parentcontext.info.username;    }      });        template.body.events({    "submit .user-search": function (event) {      // function called when new task form submitted      event.preventdefault();      meteor.call("removeallusers");      meteor.call("searchuser",  $("#username").val() );          //event.target.text.value = "";      // prevent default form submit      return false;    }    });        template.user.events({    "click .getuserbroadcast": function () {      // set checked property opposite of current value      meteor.call("getuserbroadcasts",  this._id, this.meerkatid );    }  });         }    if (meteor.isserver) {    meteor.startup(function () {      // code run on server @ startup      users.remove({});    });  }      meteor.methods({      searchuser: function (username) {      http.call("put", "https://social.meerkatapp.co/users/search?v=2",            {data: {"username": username}},            function (error, result) {              if (!error) {                 content=json.parse(result.content);                 meteor.call("getuserinfos", content.result[0]);              }            });      },      getuserinfos: function(meerkatid){         http.call("get", "https://resources.meerkatapp.co/users/"+meerkatid+"/profile?v=1.0",            {},            function (error, result) {              if (!error) {                 contentjson=json.parse(result.content);                 console.log(contentjson.result);                               users.insert(contentjson.result);              }            });     },       removeallusers: function() {          return users.remove({});    }  });

can tell me why resolves in 3 identical user entries (_id not of course) while getting console output once ?

when insert in searchuser method 2 entries. believe it's because of asynchron callbacks.

what doing wrong?

at quick glance, i'm not sure third (though i'm guessing it's related), code way is, is guaranteed run @ least twice - once on server , once on client.

in meteor documentation methods, you'll see if define method on client (referred "stub") called @ same time server method. thus, client , server both doing http.call

you want put methods meteor.isserver conditional gets called once.

if want, can put separate searchuser method definition inside meteor.isclient block, don't put http.call inside there or else want server do). in case, defined stub, , return instead of waiting results server. can simulate result server if know it's going be.

you're best trigger loading spinner (or something) when method.call executed , update results callback server, ala...

method.call("methodnameonserver", function clientcallback(results) { // something. }); 

hope helps!


Comments