【问题标题】:How to display Mongo DB collection in client main view in MeteorJS?如何在 Meteor JS 的客户端主视图中显示 Mongodb 集合?
【发布时间】:2017-06-27 17:10:21
【问题描述】:

我是 MeteorJS 的新手。我尝试使用以下代码在客户端视图中显示 MongoDB 集合。

client/main.js

Resolutions = new Mongo.Collection('resolutions');

Template.body.helpers({
   resolutions : function(){
      return Resolutions.find();
   }
});

client/main.html(此处使用blaze)

<head>
   <title>resolutions</title>
</head>

<body>
   <ul>
      {{#each resolutions}}
         {{>resolution}}
      {{/each}}
   </ul>
</body>

<template name="resolution">
   <li>{{title}}</li>
</template>

然后我使用流星 mongo shell 将一些对象插入到集合中

db.resolutions.insert({title:"test", createdAt:new Date()});

我测试天气对象被插入到集合中使用

db.resolutions.find()

输出是,

    {
     "_id": ObjectId("589c8d1639645e128780c3b4"),
     "title": "test",
     "createdAt": ISODate("2017-02-09T15:39:02.216Z")
 }

但在客户端视图中,对象标题未按预期显示在列表中。而是查看一个空屏幕。

【问题讨论】:

    标签: javascript node.js mongodb meteor meteor-blaze


    【解决方案1】:

    您似乎快到了,但似乎缺少发布和订阅您的收藏的正确声明。

    您可以在 Meteor 官方教程中找到有用的文档:https://www.meteor.com/tutorials/blaze/publish-and-subscribe

    【讨论】:

      【解决方案2】:

      假设您仍在使用autopublish,您需要在客户端和服务器上声明您的集合。最简单的方法是在 /lib 中声明它。

      /lib/collections.js

      Resolutions = new Mongo.Collection('resolutions');
      

      /client/main.js

      Template.body.helpers({
         resolutions : function(){
            return Resolutions.find();
         }
      });
      

      【讨论】:

        【解决方案3】:

        Resolutions.find(); 返回一个游标而不是一个数组。改用 fetch() 方法:

        Template.resolutions.helpers({
            resolutions: function(){
                return Resolutions.find().fetch();
            }
        });
        

        client/main.html

        <head>
           <title>resolutions</title>
        </head>
        
        <body>
            <template name="resolution">       
                <ul>
                  {{#each resolutions}}
                     <li>{{title}}</li>
                  {{/each}}
                </ul>
            </template>
        </body>
        

        【讨论】:

          猜你喜欢
          • 2016-04-15
          • 1970-01-01
          • 1970-01-01
          • 2014-02-18
          • 1970-01-01
          • 2013-05-27
          • 2014-01-16
          • 1970-01-01
          • 2011-06-21
          相关资源
          最近更新 更多