【问题标题】:Meteor.js and MongoDB, I have problems manipulating my DatabasesMeteor.js 和 MongoDB,我在操作数据库时遇到问题
【发布时间】: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


【解决方案1】:

我知道,开始学习教程是很令人沮丧的,面对它没有按预期工作,我希望以下提示可以帮助您深入了解 Meteor 及其神奇的工作原理。

  1. 确保在服务器和客户端上都注册了集合。对您来说,这意味着,确保文件 collection.js 在两个环境中运行。您可以通过将console.log() 放入文件中来确认这一点。如果您在终端和浏览器控制台中都看到输出,请继续。这在使用 autopublish 包时特别重要。
  2. 确保将数据发布到客户端。这发生在
    2.1 安装包autopublish(查看文件.meteor/packages。如果没有,可以使用meteor add autopublish安装)或
    2.2 立即跳转到手动发布数据(https://www.meteor.com/tutorials/blaze/publish-and-subscribe)。

    安装扩展Meteor DevTools for Chrome,打开开发者工具栏,切换到标签Meteor,再切换到内部标签Minimongo,确认数据已经到达浏览器。
  3. 现在 Blaze 的工作是了解更改(数据正在进入浏览器上的集合)并进行更新。

这是一篇关于Meteors publications & subscriptions 的非常好的文章。对于理解这个概念来说,它是古老的,但却是黄金。

如果您有任何问题需要来回沟通,请加入the Meteor community on Slack。

【讨论】:

  • 非常感谢您对这个非常鼓舞人心的评论和您的惊人帮助。很抱歉耽搁了,我最近赶上了其他事情,但我一定会尽快查看并接受这个答案。谢谢!!!
猜你喜欢
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 2017-04-30
  • 2022-12-07
  • 2022-11-22
  • 2015-03-04
  • 2018-06-19
  • 2021-04-19
相关资源
最近更新 更多