【问题标题】:(Meteor) #each failing with data from MongoDB(Meteor) #each 失败,来自 MongoDB 的数据
【发布时间】:2014-03-19 15:55:28
【问题描述】:

我正在使用 Meteor 制作一个测试/测验应用程序,并将所有问题、可能的答案等存储在本地 MongoDB 中,如下所示:

{
type: "someType",
skillType: "someSkillType",
questions: [ 
    {
    questionID: 1,
    question: "Some question",
    answer: 2,
    option1: "Some possible answer",
    option2: "Another one",
    option3: "Etc."
    },
    {
    questionID: 2,
    question: "Some question 2",
    answer: 1,
    option1: "Some possible answer",
    option2: "Another one",
    option3: "Etc."
    }
    ]
}

等等。我已经测试并绝对确定问题不是我的Meteor.CollectionMeteor.publish / Meteor.subscribe 或与数据库的连接。数据就在那里,我可以从控制台访问它就好了。

我的模板如下所示:

<template name="test">
    <form name="testForm" id="testForm" role="form">
        {{#each testQuestions}}
            <div class="jumbotron">
                <p><b>{{questionID}}.</b> {{question}}</p>
                    <div class="radio">
                        <label>
                            <input type="radio" name="qNumber{{questionID}}" value="1" required>
                                {{option1}}
                            </label>
                    </div>
                    <div class="radio">
                        <label>
                            <input type="radio" name="qNumber{{questionID}}" value="2">
                                {{option2}}
                        </label>
                    </div>
                    <div class="radio">
                        <label>
                            <input type="radio" name="qNumber{{questionID}}" value="3">
                                {{option3}}
                        </label>
                    </div>
            </div>
        {{/each}}
    </form>
</template>

然后我制作了一个模板助手来获取数据:

Template.test.testQuestions = function () {
    questionsAll = allQuestions.findOne({"type": "someType", "skillType": "someSkillType"}, {fields: {"_id": 0, "type": 0, "skillType": 0, "questions.answer": 0 }});
    questionsAll1 = EJSON.toJSONValue((questionsAll.questions)); // I've tried both with and without this part.
    return questionsAll1;
}

但是它不起作用,没有渲染任何内容,并且我在控制台中收到一个很长的错误,开始于:

"Exception from Deps recompute function: .observeChanges@.......

如果我直接在模板助手中创建一个静态数组,它可以正常工作。我觉得我已经尝试了一切,我根本无法找出原因。有什么帮助或想法吗?

【问题讨论】:

    标签: javascript arrays json mongodb meteor


    【解决方案1】:

    这看起来像是您需要添加警卫的情况,因为findOne 可能会在订阅准备好之前返回undefined。你可以这样做:

    Template.test.testQuestions = function() {
      var aq = allQuestions.findOne({
        type: 'someType',
        skillType: 'someSkillType'
      });
    
      if (aq && aq.questions) {
        return aq.questions;
      }
    };
    

    您无需使用EJSON.toJSONValue 转换任何内容。另请注意,您的示例数据中似乎不存在qid。也许你的意思是questionID

    【讨论】:

    • 是的,我的意思是questionID 而不是qid - 感谢您指出这一点:) 这是数据示例中的错误。我也刚刚尝试了你的建议,不幸的是没有骰子。我仍然遇到同样的(巨大的)错误。
    • 这……太棒了。有用!并不是说if 声明应该有所作为对我来说真的有意义,但显然确实如此。如果可以的话,我愿意把全世界的名声都给你——我一直在为这件事抓狂。再次感谢!
    • 太棒了 - 我很高兴我们让它工作了!有关更多信息,我还建议查看this question 的答案。简而言之,将订阅数据发送给客户端需要非零时间。如果模板在数据准备好之前呈现,您必须允许使用警卫。这是使用流星时的常见问题,我希望 1.0 的文档能够更清楚地解决这个问题。
    • 是的,不敢相信您这么快就找到了解决方案。我已经尝试了好几天才弄明白。关于您链接到的问题(我以前从未见过).. 好吧,这很奇怪,因为我实际上使用的是 Iron-router 和 waitOn: function () { return Meteor.subscribe('example') } 用于所有非全球订阅。但是,在我使用您的解决方案之前仍然无法正常工作。
    猜你喜欢
    • 2017-12-27
    • 2015-08-21
    • 2015-05-06
    • 1970-01-01
    • 2020-09-02
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多