在数组中搜索特定值

bp123

我正在尝试搜索数组以查找this._idstudentId returns但是正确的IDfav returns Object {_id: "fMNS3M5dW356C46Sq", emails: Array[1], profile: Object, roles: Array[2]}

如何获取var fav返回studentId的数组或返回NULL如果没有找到该ID?

小路: list.js

Template.list.events({
'click .favourite':function(event,template) {
      console.log('click');
        var studentId = this._id;
        var fav = Meteor.users.findOne({"profile.favorites":  studentId });

      console.log(fav);

        if (candidateId === fav) {
          Meteor.users.update({_id: Meteor.userId()}, {$pull:{"profile.favorites":studentId}});
        } else {
          Meteor.users.update({_id: Meteor.userId()}, {$push:{"profile.favorites":studentId}});
        };
    }
});
斯蒂芬·伍兹(Stephen Woods)

根据您的评论,我认为您想要:

Template.list.events({
  'click .favourite':function(event,template) {
    console.log('click');
    var studentId = this._id;
    var fav = null;

    if( Meteor.users.findOne({"profile.favorites":  studentId }) ) {
      fav = studentId;
    }
    if (candidateId === fav) {
      Meteor.users.update({_id: Meteor.userId()}, {$pull:{"profile.favorites":studentId}});
    } else {
      Meteor.users.update({_id: Meteor.userId()}, {$push:{"profile.favorites":studentId}});
    };
  }
});

如果Meteor.users.findOne返回某些内容,则会将fav设置为studentId

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在数组中搜索特定值并返回值

来自分类Dev

Python-在数组中搜索特定值

来自分类Dev

使用while循环在数组中搜索特定值

来自分类Dev

在数组中搜索并获取特定的字符串值

来自分类Dev

在数组中搜索特定值并返回其键

来自分类Dev

在数组中搜索值

来自分类Java

在数组中搜索特定的参数Java

来自分类Dev

在数组中搜索键并输出值

来自分类Dev

如何在数组中搜索值?

来自分类Dev

在数组中搜索并设置值

来自分类Dev

在数组中搜索给定值的索引

来自分类Dev

C#在数组列表中搜索特定值并返回相关值

来自分类Dev

如何在数组中搜索具有特定索引的值并修改其值?

来自分类Dev

在数组C ++中查找特定值

来自分类Dev

在数组中搜索

来自分类Dev

Javascript 在数组中搜索以某个值开头的值

来自分类Dev

在数组中搜索值匹配项并替换值-php

来自分类Dev

此代码如何在数组中搜索多个值?

来自分类Dev

如何在数组C#中搜索值的范围

来自分类Dev

在数组中搜索时如何计算值

来自分类Dev

比较数量并在数组值中搜索

来自分类Dev

在javascript中使用for循环在数组中搜索值

来自分类Dev

如何在数组输出中搜索值?

来自分类Dev

在数组内的javascript文档中搜索值

来自分类Dev

PHP - 在特定的数组值中搜索

来自分类Dev

在json数组中搜索特定值

来自分类Dev

从数组值中搜索特定词

来自分类Dev

在一维数组中搜索特定值

来自分类Dev

查找并替换特定的哈希值及其在数组中的值