【问题标题】:access data with routes使用路由访问数据
【发布时间】:2015-02-10 01:09:18
【问题描述】:

我有一个课程集

{courses{{course1_title,id,author,pages[{title,content,model, etc...},
              [ etc...]{course2,....}}

我正在尝试通过路由器在课程集合中显示当前页面的数据

this.route('page', {
    path: '/:title',
    data: function() {
    return courses.findOne({'pages.title':this.params.title};

    }

});

我想这样显示当前页面的数据:

<template name="page">
<div class ="container page">
    <h1>Page</h1>
    <!--display current page data -->
    <h3>{{title}}</h3>
    <div>{{ an other content}} etc..... </div>  
</div>

目前,路由器返回整个课程的数据,显示的标题是课程的标题。我没有找到如何访问当前页面的数据以便在页面的模板中显示它。

我试过了

return courses.findOne({'pages.title':this.params.title}{fields:{{'pages.title':this.params.title}:1}}   

还有很多其他的方式。没找到。

什么是正确的方法?

【问题讨论】:

    标签: mongodb meteor iron-router


    【解决方案1】:

    您当前的查询将在所有课程中搜索匹配的页面标题,然后返回整个课程(包括所有页面)

    您应该只返回相关页面的数据:

    course = courses.findOne({pages: { $elemMatch: { title: this.params.title }});
    return course.pages[0]
    

    顺便说一句,最好为页面创建一个单独的集合(每个页面都链接回课程 ID)。虽然这不是“Mongo”,但 Meteor 只能对集合进行响应式操作,而不是子文档。

    【讨论】:

    • 最后,我决定听从你的建议。非常感谢你,MK
    猜你喜欢
    • 2020-05-01
    • 2017-06-28
    • 2021-05-25
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    相关资源
    最近更新 更多