Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在调用addposthashtags函数后hashtagseen[]为空?_Javascript_Arrays_Mongodb_Object_Meteor - Fatal编程技术网

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

Javascript 为什么在调用addposthashtags函数后hashtagseen[]为空?,javascript,arrays,mongodb,object,meteor,Javascript,Arrays,Mongodb,Object,Meteor,我试图在帖子的hashtag[]数组中添加hashtag,作为带有num:1变量的对象添加到用户hashtagseen[]数组if它不在else如果hashtagseen[]数组中,则添加1num。如何修复代码?这是代码,高级版谢谢 编辑:我想我找不到带有this.hashtag的post.hashtag,这就是它不会转到其他位置的原因。只是猜测而已 用户对象 Accounts.createUser({ username: username, password: password

我试图在帖子的
hashtag[]
数组中添加hashtag,作为带有
num:1
变量的对象添加到用户
hashtagseen[]
数组
if
它不在
else
如果hashtagseen[]数组中,则添加1
num
。如何修复代码?这是代码,高级版谢谢

编辑:我想我找不到带有
this.hashtag
post.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}});
         }
     }
  }
});
Meteor.methods({
addposthashtags:函数(hashtags,hashtagsl){
//假设hashtagseen是一个数组,其中包含hashtagsl对象的名称
var hashtagseen=u.pull(hashtagsl,'name');
//应该在posts数组中每个hashtag运行一次。
对于(var a=0;a
我看到执行addposthashtags是在客户机中,您必须注意,因为此函数将在minimongo中执行,并且不会执行所有操作。首先,您尝试在mongo下执行此操作,如果您必须在文件夹服务器中创建一个函数,则必须执行此操作

添加Minimongo文档的文本

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

$pull-in修饰符只能接受某些类型的选择器。 不支持findAndModify、聚合函数和map/reduce。 所有这些都将在未来的版本中解决。全速前进 发行说明,请参阅存储库中的packages/minimongo/notes

Minimongo目前没有索引。这是一个罕见的例子 问题,因为客户机拥有足够的数据 索引是值得的

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

服务器:

客户:


Minimongo不支持alls操作,对于测试,如果支持,您可以在控制台中执行以测试该方法。之后,您可以直接在mongo下执行操作,这消除了您的疑虑。

我建议您尝试以下操作

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

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

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

您的最终增量更新语句如下所示


Meteor.users.update({“\id”:Meteor.userId,“profile.hashtagseen”:profile.hashtagseen[i]},{“$inc”:{“profile.hashtagseen.$.num”:1})

您的
addposthashtags
函数充满了问题。您还没有为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);
        });
      }
    }
  }
}

新代码集的新问题:

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}})
      };
    };
  };
};
});
Meteor.methods({
addposthashtags:函数(hashtags,hashtagsl){
//Issue1'hashtag'未定义,我猜您的意思是'hashtags'`
//第2号问题a的“变量”
对于(a=0;a
据我所知,您使用
Meteor.users.update
来更新用户对象,而不是我以前在客户端使用的Meteor方法。我应该把它放在服务器中的什么位置?由于某种原因,没有任何内容可以访问else语句,但hashtagseen数组中仍然没有任何内容,因此它应该是,但它似乎卡在if上。在客户端上,它
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}})
      };
    };
  };
};
});