流星铁:路由器模板不渲染

时间:2015-03-21 01:57:41

标签: meteor iron-router spacebars

我有一个主页面,其中列出了几个文本项(“Ideas”),它们是可点击的链接。单击它们可以转到可以编辑它们的页面。这是我的HTML:

<head>
    <title>Ideas</title>
</head>

<body>
</body>

<template name="Ideas">
    <ul>
        {{#each ideas}}
            {{> idea}}
        {{/each}}
    </ul>
</template>

<template name="idea">
    <li><a href="/idea/{{_id}}">{{text}}</a></li>
</template>

<template name="ShowIdea">'
    <div class="editable" contentEditable="true">{{text}}</div>
</template>

我已将Iron:Router添加到我的项目中以允许在页面之间移动。这是javascript:

Ideas = new Mongo.Collection("ideas");

if (Meteor.isClient) {
    Router.route('/', function() {
        this.render('Ideas');
    });

    Router.route('/idea/:_id', function() {
        var idea = Ideas.findOne({_id: this.params._id});
        this.render('ShowIdea', {text: idea.text});
    });

    Template.Ideas.helpers({
        ideas: function () {
            return Ideas.find({});
        }
    });
}

我使用Meteor Mongo命令行工具向Mongo DB插入了一个想法。该单个项目在我的主页面上正确显示。以下是我在主页调试器中的HTML样式:

<html>
    <head>...</head>
    <body>
        <ul>
            <li>
                <a href="/idea/ObjectID("550b7da0a68cb03381840feb")">The first idea ever</a>
            </li>
        </ul>
    </body>
</html>

点击该链接,我将转到一个地址为:

的新页面

http://localhost:3000/idea/ObjectID(%22550b7da0a68cb03381840feb%22)

但页面上没有任何内容。在调试器控制台中,我看到此错误消息+堆栈跟踪,但它对我来说没有任何意义,因为它似乎与铁路由器和流星有关,而不是我实际写的代码:

Exception in callback of async function: http://localhost:3000/Idea.js?2fd83048a1b04d74305beae2ff40f2ea7741d40d:10:44
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
onRerun@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:520:13
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
onRun@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:505:15
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:448:7
_runRoute@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:543:17
dispatch@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:844:27
route@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:710:19
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:371:18
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:448:7
http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:390:21
_compute@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:308:36
Computation@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:224:18
autorun@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:499:34
http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:388:17
nonreactive@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:525:13
dispatch@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:387:19
dispatch@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:1688:22
onLocationChange@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:1772:33
_compute@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:308:36
_recompute@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:322:22
flush@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:452:24

然后以此警告消息结束:

  

路线调度从未呈现过。您是否忘记在onBeforeAction中调用this.next()?

我没有onBeforeAction(我甚至不确定那是什么)......所以我认为这个消息不属于我?

我刚开始使用Meteor,并且刚刚在24小时前添加了铁路由器,所以我在这里有点迷失。关于我如何调试和解决这个问题的任何指针都会很棒。

1 个答案:

答案 0 :(得分:2)

需要解决两件事:

  1. 从shell中插入文档时,会为它们分配_id个值为mongo ObjectID的值,而meteor默认使用字符串。这解释了奇怪的URL。为避免此问题,通常最好从服务器初始化数据。这是一个例子:
  2. if (Meteor.isServer) {
      Meteor.startup(function() {
        if (Ideas.find().count() === 0) {
          Ideas.insert({text: 'feed the cat'});
        }
      });
    }
    

    现在在$ meteor reset之后,你将始终以一个与猫有关的想法开始。

    1. 如果您希望将上下文传递给模板,则需要使用data属性,如下所示:
    2. Router.route('/idea/:_id', function() {
        this.render('ShowIdea', {
          data: function () {return Ideas.findOne({_id: this.params._id})}
        });
      });
      

      请参阅文档中的this example。进行这些更改后,代码对我来说正常工作。