【问题标题】:Meteor: insert produces multiple entries流星:插入产生多个条目
【发布时间】:2015-05-08 09:09:33
【问题描述】:

我的问题如下。

我开始使用流星并编写了一些针对猫鼬的小代码。

我想将用户插入我的数据库。我首先必须使用 meerkatId 的用户名调用 meerkat,然后才能获取用户信息。

这是我的代码。

Users = new Mongo.Collection("users");


if (Meteor.isClient) {

Template.body.helpers({
  users: function () {
      // Otherwise, return all of the tasks
      return Users.find({});
  }

});

Template.user.helpers({
  parentUsername: function (parentContext) {

      return parentContext.info.username;
  }
  
});

  
Template.body.events({
  "submit .user-search": function (event) {
    // This function is called when the new task form is 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 the checked property to the opposite of its current value
    Meteor.call("getUserBroadcasts",  this._id, this.meerkatId );
  }
});

   
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at 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({});
  }
});

有人能告诉我为什么这会在 3 个相同的用户条目(当然不是 _id)中得到解决,同时只获得一次控制台输出吗?

当我在 searchUser 方法中插入时,我得到 2 个条目。我相信这是因为异步回调。

我做错了什么?

【问题讨论】:

    标签: mongodb meteor insert callback call


    【解决方案1】:

    快速浏览一下,我不确定第三个(尽管我猜它是相关的),但是按照现在的代码,保证至少运行两次- 一次在服务器上,一次在客户端上。

    Meteor documentation for methods 中,您会看到如果您在客户端定义方法(称为“存根”),它将与服务器方法同时调用 .因此,您的客户端和服务器 BOTH 都在执行HTTP.call

    您希望将方法放入 Meteor.isServer 条件条件中,以便只调用一次。

    如果你愿意,你可以在Meteor.isClient 块内放置一个单独 searchUser 方法定义,但不要将HTTP.call 放在其中或您希望服务器执行的任何其他操作)。在这种情况下,它将被定义为一个存根,并将立即返回,而不是等待服务器的结果。如果你知道它会是什么,你可以这样做来模拟来自服务器的结果。

    您可能最好在执行 Method.call 时触发加载微调器(或其他东西)并使用来自服务器的回调更新结果,唉...

    Method.call("methodNameOnServer", function clientCallback(results) { // do something. });
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 2014-07-09
      • 2023-03-11
      • 2021-02-21
      • 2015-12-11
      • 2013-05-22
      • 2023-04-11
      相关资源
      最近更新 更多