【发布时间】:2020-09-22 15:07:14
【问题描述】:
我对 MongoDB 和 Meteor.js 都非常陌生。我正在尝试按照教程进行操作,尽管事实上我正在复制老师所做的事情,但我还是被卡住了。我正在尝试显示数据库entries 中的项目列表,但没有显示任何内容。我尝试了很多方法,并且 console.logged 很多,但我从来没有设法让它工作。
这是我的文件:
1/main.html
<body>
<h3 class="title">Welcome!</h3>
<div class="container list">
<ul class="collection">
{{#each entries}}
{{> list}}
{{/each}}
</ul>
</div>
</body>
<template name="list">
<li class="collection-item"> {{title}}
</li>
</template>
2/main.js
import { event } from 'jquery';
import { Template } from 'meteor/templating';
import { Entries } from '../lib/collection.js';
import './main.html';
Template.body.helpers({
entries(){
return [Entries.find({})];
},
});
3/collection.js
import { Mongo } from 'meteor/mongo';
export const Entries = new Mongo.Collection('entries');
这是我在运行meteor mongo后在命令行中创建的数据库,当我输入命令时:db.entries.find().pretty()
{
"_id" : ObjectId("5f69ef84eda325daca4b0d51"),
"title" : "Hello",
"content" : "try hard"
}
{
"_id" : ObjectId("5f69f0db434cda7b7f21d193"),
"title" : "Hello",
"createdAt" : ISODate("2020-09-22T12:40:59.924Z")
}
{
"_id" : ObjectId("5f69f1c8434cda7b7f21d194"),
"title" : "Hello",
"createdAt" : ISODate("2020-09-22T12:44:56.230Z")
}
{ "_id" : ObjectId("5f69f2c3434cda7b7f21d195"), "name" : "Watercress" }
{
"_id" : ObjectId("5f69f6012a6382ef314d84cb"),
"title" : "goodbye",
"createdAt" : ISODate("2020-09-22T13:02:57.511Z")
}
{
"_id" : ObjectId("5f69f6bf2a6382ef314d84cd"),
"title" : "fresh",
"createdAt" : ISODate("2020-09-22T13:06:07.561Z")
}
如果你能帮忙,那就太好了!
【问题讨论】:
-
您是否尝试记录数据库,它是否包含数据?
-
return Entries.find({});
-
是的对不起它曾经是
return Entries.find({});我不知道为什么我改变了它。不幸的是,它仍然无法正常工作。我试图对数据库进行控制台登录,但没有取得多大成功。当我 console.log(Entries.find());我得到这样的东西:光标集合:LocalCollection {name:“条目”,_docs:_IdMap,_observeQueue:M...r._SynchronousQueue,next_qid:2,查询:{...},...}字段:未定义的限制:未定义的匹配器:匹配器{_paths: {…}, _hasGeoQuery: false, _hasWhere: false, _isSimple: true, _matchingDocument: undefined, …} reactive: true 等等。 -
是的,这是一个光标。如果你想查看你需要的文件
console.log(Entries.find().fetch())。为了将文件发送给您需要publish them on the server and subscribe on the client 的客户,您这样做了吗?如果是这样,请将他们的代码添加到问题中。 -
不,我认为我没有这样做。我只是按照本教程进行操作,此时我对发布到服务器一无所知:meteor.com/tutorials/blaze/creating-an-app 抱歉,这对我来说都是全新的。
标签: javascript mongodb meteor