【问题标题】:Meteor: Iterate over cursor from MongoCollection.find() and print a fieldMeteor:从 MongoCollection.find() 遍历光标并打印一个字段
【发布时间】:2016-12-02 04:26:10
【问题描述】:

我无法打印从流星集合中获取的内容。我已经在 /ui/api/collections.js 中声明了我的收藏

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import 'meteor/aldeed:collection2';

export const Courses = new Mongo.Collection("courses");

Courses.allow({
    'insert': function () {
       // if (Meteor.isEducator(Meteor.userId())) {
            return true;
    }
})

/imports/api/course.html

<template name="course">
    <h2> You are enrolled in the following courses:</h2>
    <ul>
        {{#each course_list}}
            {{> getCourseName}}
        {{/each}}
    </ul>
</template>

<template name="getCourseName">
    <li><h2>{{courseName}}</h2></li>
</template>

/imports/api/course.js

Template.course.helpers({
    course_list: function() {
        var result =  Courses.find({});
        console.log("result is:");
        console.log(result);

        return result;
    }
})

我已将 Courses 声明为新的 Mongo.Collection 并将其导入 /server/main.js 然后我将 /imports/api/collections.js 导入 course.js 和 course.html。

the console output of printing course_list's result is :
L…n.Cursor {collection: LocalCollection, sorter: null, matcher: M…o.Matcher, _selectorId: undefined, skip: undefined…}

我注意到它说 LocalCollection 这让我觉得它没有找到实际的集合。当我在服务器端 mongo 控制台上执行 db.courses.find() 时,我看到集合中确实存在两个课程,并且它们都有一个 courseName 字段。我认为我不需要发布/订阅,因为我在导入中声明集合,然后将集合导出为全局变量。我一般是流星和javascript的新手,因此非常感谢任何解释。

【问题讨论】:

    标签: javascript mongodb meteor handlebars.js


    【解决方案1】:

    不用担心。您已经可以访问数据。只是 Meteor 在客户端创建了local collections - Minimongo 的强大功能。这就是为什么您在控制台上看到的是 LocalCollection 而不是 Courses,这可能是您所期待的。

    还要注意,集合上的find 返回一个cursor。需要使用fetchforEachmap等操作来操作实际数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      相关资源
      最近更新 更多