【问题标题】:Why is hashtagseen[] empty after I call the addposthashtags function?为什么我调用 addposthashtags 函数后 hashtagseen[] 为空?
【发布时间】:2014-10-31 19:17:42
【问题描述】:

我正在尝试在帖子的hashtag[] 数组中添加主题标签作为具有num:1 变量的对象到用户hashtagseen[] 数组if 它还没有在其中else 添加1 num如果主题标签已经在 hashtagseen[] 数组中。如何修复我的代码?这是代码,谢谢。

编辑:我想我没有找到post.hashtagthis.hashtag,这就是为什么它不会去别的地方。只是猜测。

用户对象

Accounts.createUser({
    username: username,
    password: password,
    email: email,
    profile: {
        hashtagsl:[],
    }
});

collections/post.js

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

调用它

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

lib/usershash

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}});
         }
     }
  }
});

【问题讨论】:

    标签: javascript arrays mongodb object meteor


    【解决方案1】:

    您的addposthashtags 函数充满了问题。您还没有为主题标签对象提供“模式”。

    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}})
          };
        };
      };
    };
    });
    

    【讨论】:

    • 它在客户端运行并引发此错误Error invoking Method 'userViewedHashTags': Method not found [404] debug.js:41。它在服务器上运行并且什么都不做。
    • 删除了示例代码;首先修复您自己代码中的现有问题 - 如果您仍有问题,我们可以进一步调试。
    • 我尝试尽我所能调试错误,所以在修复之前我无法测试它是否有效,但我不知道我没有看到什么导致错误。
    • @noah 刚刚添加了一组新的 cmets。你用什么来编辑?你试过Webstorm吗?还是有不错的语法验证?
    • 也许,在函数的开头,展平可见标签列表以便于搜索:var hashtagsseen = _.pluck(hashtagsl, 'name')。然后使用 indexOf 查看当前主题标签是否在列表中,例如。 var hashtagAlreadySeen = (-1 != _.indexOf(hashtagseen, hastags[a])).
    【解决方案2】:

    我看到执行addposthashtags是在客户端,一定要注意,因为这个函数会在minimongo中执行,并不是所有的操作都起作用。首先,您尝试在 mongo 下执行此操作,如果可以,您必须在文件夹服务器中创建一个函数。

    添加 Minimongo 文档的文本

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

    $pull in 修饰符只能接受某些类型的选择器。 不支持 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下执行操作了,你的疑惑就迎刃而解了。

    【讨论】:

    • 据我了解,您使用 Meteor.users.update 而不是流星方法更新用户对象,我之前已经在客户端完成过。我会把它放在服务器的什么下面?
    【解决方案3】:

    我建议您尝试以下方法

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

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

    2) 在我看来,增加视图的更新似乎也不起作用。这里需要注意几件事 - $set:{'profile.hashtagseen[i]':num++} - MongoDB 将无法识别 'profile.hashtagseen[i]' 中的 'i' 并且 'num++' 不是在 Mongo 中完成增量的方式。 我建议您查看 $inc 和 MongoDB 的 positional update 文档。

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

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

    【讨论】:

    • 由于某种原因,else 语句没有任何内容,但 hashtagseen 数组中仍然没有任何内容,所以它应该是,但它似乎卡在 if 上。
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2020-05-13
    相关资源
    最近更新 更多