【问题标题】:Can't get any db data out of Meteor无法从 Meteor 中获取任何数据库数据
【发布时间】:2015-06-30 00:15:56
【问题描述】:

我完成了 Meteor 的“简单待办事项”教程,并认为我已经了解了它的工作原理。但是,我现在开始自己的项目,并立即遇到问题。

无论我尝试什么,我都无法从 Meteor 中获取任何数据并进入我的模板,甚至无法进入 console.log。总是undefined。我什至在终端中通过meteor mongo 仔细检查了集合,并且我传入的数据是正确的。我什至尝试过硬编码文档 ID。

我希望有人能指出我的代码明显有什么问题。

这是当前不起作用的迭代,但我似乎已经尝试了我在网上找到的所有变体:

JS:

Pros = new Mongo.Collection("pros");

if (Meteor.isClient) {
  Meteor.subscribe("pros");
  Router.configure({
    layoutTemplate: 'main'
  });
  Router.route('/brief/:_id', function () {
    this.render('Brief', {
      data: function(){
            var proid = this.params._id;
            return Pros.findOne({ _id: proid });
        }
    });
  });
}
if(Meteor.isServer){
  Meteor.publish("pros", function () {
    return Pros.find();
  });
}

HTML:

<template name="main">
    <h1>Title here</h1>
    {{> yield}}
</template>

<template name="brief">
  <h2>{{ name }}</h2>
</template>

(我也在 pros 的 foreach 中尝试过上述方法)

数据库数据(通过终端中的查找命令)

db.pros.find({});
{ "_id" : ObjectId("55915761145c29018ac97edb"), "name" : "Jimbob" }

我现在还尝试了以下方法,但没有产生任何不同的结果(它只是保持加载状态):

Pros = new Mongo.Collection("pros");

if (Meteor.isClient) {
  Router.configure({
    layoutTemplate: 'main'
  });
  Router.route('/brief/:_id', {
    loadingTemplate: 'loading',
    waitOn: function () {
      return Meteor.subscribe('pros', this.params._id);
    },
    action: function () {
      this.render('brief');
    }
  });
}
if(Meteor.isServer){
  Meteor.publish('pros', function(proId) {
    return Pros.findOne(proId);
  });
}

有什么想法吗?

【问题讨论】:

  • 请同时显示您的模板和/或您用于打印到控制台的代码。
  • 另外,您使用的是哪个路由器?铁路由器?
  • 是的,铁路由器 - 我现在将粘贴一些模板代码,但我认为它不会增加讨论:)
  • 在模板代码中添加,我还粘贴了另一个我尝试过但没有更好结果的解决方案(直接来自铁路由器文档)。
  • 通过蒙古atmospherejs.com/msavin/mongol查看您的订阅状态

标签: mongodb meteor iron-router


【解决方案1】:

您在 Meteor 中使用 ObjectID,这不是默认设置(Meteor 在插入文档时生成 17 个字符串而不是 Mongo ObjectID)

尝试类似:

data: function () {
    Pros.findOne({ _id: new Mongo.ObjectID(this.params._id)});
}

【讨论】:

  • 啊哈!我通过命令行插入了文档(尽管使用了meteor mongo),所以我猜这就是导致打嗝的原因。如果您需要通过ID引用,是否真的可以通过其他方式插入?似乎有点奇怪的限制?
  • 哇——成功了!
  • 您仍然可以从命令行插入,但您需要设置一个 _id 以防止 Mongo 生成它自己的。在浏览器控制台中调用 Random.id() 可以解决问题,但仍然有点手动。
  • 出于这个原因,我不久前创建了来自流星的随机代码的要点。它只是咖啡脚本和服务器端,但可能对您有用...gist.github.com/mjmasn/e8bd17d89a02d085c06b
【解决方案2】:

您不应在发布中使用findOne。它只返回一个对象,而不是游标。此外,将data 字段添加回您的客户端路由。试试这个:

if (Meteor.isClient) {
  Router.configure({
    layoutTemplate: 'main'
  });
  Router.route('/brief/:_id', {
    loadingTemplate: 'loading',
    waitOn: function () {
      return Meteor.subscribe('pros', this.params._id);
    },
    data: function(){
      var proid = this.params._id;
      return Pros.findOne({ _id: proid });
    },
    action: function () {
      this.render('brief');
    }
  });
}

if(Meteor.isServer){
    Meteor.publish('pros', function(proId) {
        return Pros.find({_id: proId});
    });
}

【讨论】:

  • 好的,这已经让我超过了加载状态,但是 {{ name }} 仍然输出空白 - 我会进一步调查。
  • return Pros.find({}); 替换publish 语句中的return Pros.find({_id: proId}); 至少将整个集合带入模板(使用msavin:mongol 验证) - 所以 findOne 语句有问题吗?但我使用的 ID 绝对是集合中唯一的 ID:/
  • 虽然我刚刚注意到文档 ID 被标记为 _str 类型,但不应该是 ObjectId 吗?
猜你喜欢
  • 1970-01-01
  • 2017-01-31
  • 2017-09-21
  • 2020-11-30
  • 2018-05-28
  • 2012-01-06
  • 2021-10-25
相关资源
最近更新 更多