在调用addposthashtags函数后,为什么hashtagseen []为空?

时间:2014-09-07 11:08:11

标签: javascript arrays mongodb object meteor

我正在尝试将帖子的hashtag[]数组中的主题标签作为带有num:1变量的对象添加到用户hashtagseen[]数组if,但它尚未包含在else数组num {1}}如果主题标签已在hashtagseen[]数组中,则添加post.hashtag。如何修复代码?这是代码,谢谢先进。

编辑:我认为我没有找到this.hashtag Accounts.createUser({ username: username, password: password, email: email, profile: { hashtagsl:[], } }); ,这就是为什么它不会转到其他地方。只是一个猜测。

用户对象

var post = _.extend(_.pick(postAttributes, 'title', 'posttext','hashtags'), {
   userId: user._id, 
   username: user.username, 
   submitted: new Date().getTime(),
   commentsCount: 0,
   upvoters: [], votes: 0,
 });

集合/ post.js

Meteor.call('addposthashtags',this.hashtags,Meteor.user().profile.hashtagsl);

调用它

Meteor.methods({
 addposthashtags: function (hashtags,hashtagsl) {
    //supposed to make hashtagseen a array with the names from the hashtagsl object in it
    var hashtagseen = _.pluck(hashtagsl, 'name');
    //supposed to run once per each hashtag in the posts array.
    for (var a = 0; a < hashtags.length; a++) {
        //supposed set hashtagnumber to the number indexOf spits out.
        var hashnumber=hashtagseen.indexOf(hashtags[a]);
        //supposed to check if the current hashtag[a] === a idem in the hashtagseen.
         if(hashnumber===-1){
              var newhashtag = this.hashtags[a];
              //supposed to make the object with a name = to the current hashtags
              Meteor.users.update({"_id": this.userId},{"$push":{"profile.hashtagsl": {name: newhashtag, num: 1}}})
         } else {
              var hashi = hashtagseen[hashnumber];
              //supposed to ad one to the num variable within the current object in hashtagsl
              Meteor.users.update({"_id": this.userId, "profile.hashtagsl.name":hashi},{"$inc":{"profile.hashtagsl.num":1}});
         }
     }
  }
});

lib / usershash

{{1}}

3 个答案:

答案 0 :(得分:0)

我看到在客户端执行addposthashtags,你必须要注意,因为这个函数将在minimongo中执行,并且不能完成所有操作。首先你尝试在mongo下执行这个操作,如果它工作你必须在文件夹服务器中创建一个函数。

添加Minimongo文档的文本

在此版本中,Minimongo有一些限制:

  

$ pull in modifiers只能接受某些种类的选择器。   不支持findAndModify,聚合函数和map / reduce。   所有这些都将在未来的版本中得到解决。完整的Minimongo   发行说明,请参阅存储库中的packages / minimongo / NOTES。

     

Minimongo目前没有索引。这很难成为一个   问题,因为客户有足够的数据,这是不寻常的   索引是值得的。

您尝试使用相同的操作在服务器上创建一个方法。

  

服务器:

Meteor.methods({
  updateUser: function (newhashtag) {
    Meteor.users.update(this.userId, 
                       {
                          $addToSet: {'profile.$.hashtagseen': newhashtag}
                       });
  }
});
  

客户端:

Meteor.call('updateUser',newhashtag,function(err,result){
        if (err)
                  console.log(err);// there you can print the erro if there are


});

Minimongo不支持alls操作,为了测试你可以在控制台中执行以测试方法(如果支持)。之后你可以直接在mongo下执行操作,这可以解决你的疑虑。

答案 1 :(得分:0)

我建议您尝试以下

1)假设在newhashtag=hashtag[a]之后你在newhashtag变量中得到一个JSON对象,尝试用newhashtag:{num:1};替换newhashtag.num = 1 - 这会将num变量添加到对象并设置值。

1.a)出于调试目的,尝试在您设置和更改newhashtag变量的两行中的每一行之后添加一些console.log(JSON.stringify(newhashtag)); - 这样您就可以确切地知道自己是什么&#39 ;尝试添加到mongoDB文档。

2)增加视图的更新在我看来也不会起作用。这里需要注意的一些事情 - $set:{'profile.hashtagseen[i]':num++} - MongoDB无法确定“我是谁”。在&#39; profile.hashtagseen [i]&#39;和#num;&#39;不是如何在Mongo中完成增量。 我建议您查看MongoDB的$incpositional update文档。

您的最终增量更新语句将类似于

Meteor.users.update({"_id": Meteor.userId, "profile.hashtagseen": profile.hashtagseen[i]}, {"$inc":{"profile.hashtagseen.$.num":1}});

答案 2 :(得分:0)

您的addposthashtags功能充满了问题。你还没有提供一个&#34;架构&#34;对于hashtag对象。

addposthashtags: function () {
  for (a = 0; a < this.hashtag.length; a++) {
    // Issue1: You're querying out the user for every iteration of the loop!?
    for (i = 0; i < Meteor.user().profile.hashtagseen.length; i++) {
      // Issue2: You're comparing two _objects_ with ===
      // Issue3: Even if you use EJSON.equals - the `num` property wont match
      // Issue4: You're querying out the user again?
      if (this.hashtag[a] === Meteor.user().profile.hashtagseen[i]) {
        // Issue5 no `var` statement for hashtagseeni?
        // Issue6 You're querying out the user again??
        hashtagseeni = Meteor.user().profile.hashtagseen[i];
        //Issue7 undefined hashtagsli?
        //Issue8 Calling multiple methods for the one action (eg in a loop) is a waste of resources.
        Meteor.call('addseen', hashtagsli);
      } else {
        //Issue9 no `var` statement for newhashtag?
        newhashtag = this.hashtag[a];
        newhashtag.num = 1;
        //Issue8b Calling multiple methods for the one action (eg in a loop) is a waste of resources.
        Meteor.call('updateUser', newhashtag, function (err, result) {
          if (err)
            console.log(err);
        });
      }
    }
  }
}

此外,该方法也有类似的问题:

addseen: function (hashtagseeni) {
  // Issue10: var `profile` is undefined
  // Issue11: should use `this.userId`
  // Issue12: hashtagseeni wouldn't match profile.hashtagseen due to "num" field.
   Meteor.users.update({"_id": Meteor.userId, "profile.hashtagseen": profile.hashtagseeni}, {"$inc":{"profile.hashtagseen.$.num":1}});
}

您的新代码集的新问题:

Meteor.methods({
     addposthashtags: function (hashtags,hashtagsl) {
  //Issue1 `hashtag` is undefined, guessing you mean `hashtags`
  //Issue2 no `var` for a
  for (a = 0; a < hashtag.length; a++) {
    //Issue3 no `var` for i
    //Issue4 Why are you looping through both? 
    // don't you just want to check if hashtag[a] is in hashtagsl? 
    for (i = 0; i < hashtagsl.length; i++) {
      if (hashtags[a] === hashtagsl[i].name) {
        var hashi = hashtagsl[i].name;
        //supposed to ad one to the num variable within the current object in hashtagsl.
        // Issue5: This query wont do what you think. Test until you've got it right.
        Meteor.users.update({"_id": Meteor.userId, 'profile.hashtagsl':hashi}, {"$inc":{"num":1}});
      } else {
        // Issue6 `this.hashtag` isn't defined. guessing you mean `hashtags[a]`
        var newhashtag = this.hashtag[a];
        // Issue7 superfluous statement 
        var newhashtagnum = num = 1;
        // Issue8 Obvious syntax errors
        //   Perhaps try Meteor.users.update({"_id": this.userId},{"$push":{"profile.hashtagsl": {name: newhashtag, num: 1}}}) 
        Meteor.users.update({"_id": Meteor.userId, 'profile'},{"$addToSet":{"hashtagsl"[newhashtag]=newhashtagnum}})
      };
    };
  };
};
});