gpt4 book ai didi

javascript - Meteor:从 MongoCollection.find() 遍历光标并打印字段

转载 作者:行者123 更新时间:2023-12-03 06:26:34 24 4
gpt4 key购买 nike

我在打印从 meteor 收藏中获得的内容时遇到问题。我已在/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() 时,我看到集合中确实存在两个类(class),并且它们都有一个 courseName 字段。我认为我不需要发布/订阅,因为我在导入中声明集合,然后将集合导出为全局变量。一般来说,我对meteor和javascript都很陌生,所以非常感谢任何解释。

最佳答案

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

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

关于javascript - Meteor:从 MongoCollection.find() 遍历光标并打印字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38625169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com