【问题标题】:How to fetch and display the data from the database?如何从数据库中获取和显示数据?
【发布时间】:2016-03-11 15:33:56
【问题描述】:

我正在尝试从我的收藏中获取并显示这些项目。 我创建了模板并为每个项目提供了设计。代码如下:

<template name="list_products">

    {{#each applications}}
        <div class="col-sm-4 col-lg-4 col-md-4">
            <div class="thumbnail">
                <img src="http://placehold.it/320x150" alt="">
                    <div class="caption">
                         <h4 class="pull-right">{{price}}</h4>
                         <h4><a href="#">{{title}}</a>
                                    </h4>
                                    <p>{{description}}</p>
                     </div>
            </div>
        </div>
    {{/each}}
</template>

在 .js 文件中,我创建了将返回集合中所有项目的应用程序

Template.list_products.applications = function(){
    Products.find();
}

然后我在 .html 文件中调用了模板

           {{> list_products}}

运行“npm run start”后出现此错误

Refused to execute script from 'http://localhost:3000/js/jquery.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled

我做错了什么?这里缺少任何步骤吗?

【问题讨论】:

  • 问题是什么都不显示还是数据有误?如果你打开一个控制台,Products.find().count() 会给你什么?
  • 我猜你的问题可能是Template.list_products.applications 部分。试试Template.list_products.helpers.applications = function(){ Products.find(); }。我假设您已经自动发布或已经发布了。
  • @DavidWeldon 我得到了 3,集合中的文档数
  • @Oscar 好的,现在试试
  • @Oscar 没用。 t 没有自动发布,我没有发布。我将我的集合放在 /lib 目录中,以供客户端和服务器访问。我还需要发布和订阅吗?

标签: mongodb meteor meteor-collection2


【解决方案1】:

使用助手来完成。

    Template.list_products.helpers({
     applications:function(){
      return Products.find({});
      }
    });

您可以再次遍历应用程序,因为它会返回一个游标。也可以return Products.find({}).fetch(); 但首先尝试使用光标,看看它是否有效。

【讨论】:

  • 谢谢它的工作,接受你的回答。一个问题,如果你知道答案。我怎样才能让每个项目都可以点击?我想让用户点击它进入项目预览页面。 :)
  • 有几个步骤可以使其可点击。首先,制作您要用于显示单个帖子的路线,通常是:posts/{{_id}},然后通过制作其模板进一步进行。渲染路由时,将会话变量设置为 this.params._id。通过这种方式,您将能够通过其 id 从数据库中提取单个条目。我无法在这里编写整个代码,但请按照这些步骤并查看一些示例。
  • 当我看到这样的问题时,我很喜欢。两者兼而有之
猜你喜欢
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 2015-07-12
相关资源
最近更新 更多