【问题标题】:Loop over the Mongodb objects to get the values in Meteorjs遍历 Mongodb 对象以获取 Meteorjs 中的值
【发布时间】:2013-12-11 14:01:26
【问题描述】:

我是第一次使用 Meteorjs 和 MongoDB。我正在开发一个问答应用程序。我有一个使用 Meteor 数据库调用获得的对象列表。我想遍历列表并希望将其属性之一拆分为长度 50,然后想将这些拆分对象的列表发送到数据库。我所拥有的是这个

Template.questions.questions = function () {
questions= Meteor.questions.find({topic_id: Session.get("currentTopicId")});
return questions
}
问题列表中的每个问题对象都有一个属性 question_text。使用循环我想将此属性拆分为 50 的长度,然后想将其推送到一个空列表并返回该列表。喜欢
questions_list=[]
for(i=0;i<questions.length;i++){
    question=questions[i].question_text[0:50]   // python syntex to get first 50 char of a string
    questions_list.push(question
}
return questions_list

我的 HTML 就像

<tbody>
            {{#each questions}}
            <tr>
                <td class="span2" style="overflow:hidden;width:50px">
                    <a href="#" data-id="{{_id}}" class="edit"> {{question_text}}</a>
                </td>

            </tr>

            {{/each}}

            </tbody>

建议我如何在meteorjs 中实现这一点。我的问题是,当我尝试遍历此问题列表时,有许多属性,例如集合、结果、查询。在这里我无法迭代这个对象列表。

我怎样才能得到meteorjs抛出的错误信息

【问题讨论】:

    标签: mongodb meteor handlebars.js meteorite


    【解决方案1】:

    这将为您提供与查询匹配的所有问题的简短文本列表:

    var questions = Questions.find({...}).map(function(question) {
        return question.text.substr(0,50);
    });
    

    您可以直接在助手中使用它:

    Template.questions.questions = function () {
        return Questions.find({...}).map(function(question) {
            return question.text.substr(0,50);
        });
    };
    

     


     

    顺便说一句,Questions.find({...}) 不返回问题的列表,而是一个游标对象,您可以使用它以有效的方式操作查询数据(如上面的 map 方法) .要获取原始数组,您需要在该光标上使用 fetchQuestions.find({...}).fetch()

    【讨论】:

    • 好的,这就是问题所在。在今天之前,我只使用请求返回数组的 django。同样我在这里尝试。非常感谢。我做了 console.log(question.question_text.substr(0,50)) 它正在工作。但是没有任何内容返回到模板。谢谢休伯特 OG
    • 我认为错误出在我的模板中。看看模板对吗?
    • 如果您使用上面 Hubert 提供的Template.questions.questions,那么它会返回一个字符串列表。我注意到在您的模板中,您希望数组中的每个元素都有一个 {{_id}} 和一个 {{questionText}} 属性。为此,请尝试:Template.questions.questions = function() { return Questions.find({...}).map(function(question) { return {_id: question._id, questionText: question.text.substr(0,50)}; }); }
    • 确实如此。您在地图回调中返回的是给模板的内容。如果你返回一个字符串,你只能通过调用{{this}}来显示它。如果要使用多个值,请返回 Bruce 所写的对象。
    • 谢谢 Bruce,谢谢 HUbert OG,是的,我将多个值作为键值对字典返回,但在模板中我仍然无法呈现这些。我正在检查console.log。完整的对象在那里。但不在模板上渲染。
    【解决方案2】:

    如果您只是显示数据,您可以创建一个车把助手:

    Template.questions.helpers({
      first50: function(question) {
        return question.text.substr(0,50);
      }
    });
    

    然后添加助手:

    Template.questions.questions = function() {
      return Questions.find({...});
    };
    

    然后在你的模板中你可以这样做:

    <tbody>
      {{#each questions}}
        <tr>
          <td class="span2" style="overflow:hidden;width:50px">
            <a href="#" data-id="{{_id}}" class="edit"> {{first50 question_text}}</a>
          </td>
        </tr>
      {{/each}}
    </tbody>
    

    【讨论】:

    • 谢谢布鲁斯哈伯德。如果我正在尝试您提供的解决方案,它会给我重新计算问题异常。
    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 2011-08-03
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    相关资源
    最近更新 更多