定义一个函数以匹配Meteor中的文档数组元素与文档值

时间:2016-09-02 23:29:00

标签: javascript jquery mongodb meteor

我一直致力于流星网络应用。我在MongoDB中插入了一个文档。

questionsList.insert({ask: "When was the War of 1812", answer: "1812", possAnsw: ["1811", "1812", "1819", "1820", "1840"]})

当一个人从可能的答案中选择正确的答案时,我希望有一个功能匹配。问题是我很难访问一个值并将其与特定<li>标记匹配。

的伪代码:

if (li.currentTarget == questionList.find({answer: 1}).fetch() {
       $(li.currentTarget).css("background-color", "yellow");
}

基本上,只有当我点击正确答案时才会突出显示:

Template.questions.events({
      'click .answers':function(li) {

        $(li.currentTarget).css('background-color', "yellow").css("width", "fit-content");

      }

    });

现在,它突出显示每个<li>元素,无论它是否与正确答案匹配(即“询问”)。问题是我似乎无法找到如何获取“ask”键的值并将其与li.currentTarget匹配。

我看了几篇以下文章但无济于事:

Get particular element from mongoDB array

Retrieve only the queried element in an object array in MongoDB collection

1 个答案:

答案 0 :(得分:0)

如果我正确地得到你的问题,基本上当用户点击问题的答案时,服务器会检查答案是否正确..所以,我认为你应该查询问题而不是答案

var currentQuestionObject = questionList.find({ask: "When was the war of 1812?"}).fetch()

if (li.currentTarget == currentQuestionObject.answer) {
   $(li.currentTarget).css("background-color", "yellow");
}