【问题标题】:Meteor Iron:Router Template not RenderingMeteor Iron:路由器模板未渲染
【发布时间】:2015-05-24 13:32:25
【问题描述】:

我有一个主页,其中列出了一些文本项(“想法”),它们是可点击的链接。单击它们应该会将您带到可以编辑它们的页面。这是我的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)

但页面上没有显示任何内容。在调试器控制台中,我看到了这条错误消息 + 堆栈跟踪,但这对我来说毫无意义,因为这一切似乎都与 Iron-router 和 meteor 相关,而不是我实际编写的代码:

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 小时前刚刚添加了 Iron-router,所以我有点迷路了。任何有关如何调试和修复此问题的指示都会很棒。

【问题讨论】:

    标签: meteor iron-router spacebars


    【解决方案1】:

    有两件事需要解决:

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

    现在在$ meteor reset 之后,你总是会从一个与猫有关的想法开始。

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

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

    【讨论】:

    • 太棒了!我认为 Mongo 生成的 _id 看起来有点奇怪,但我实际上并不怀疑它会导致任何问题(名称中的各种特殊字符都被转义了,所以我认为这很好)。我应该知道将参数称为data...我所遵循的示例将其称为data,但我认为我可以随心所欲地称呼它,而且名称并不重要。现在我知道得更多了。谢谢!
    • 实际日志输出中是否有任何提示您或您如何找出问题所在?
    • 我希望我能在这里说一些有见地的东西,但我没有。我在尝试代码之前解决的第一个问题(这很常见)。 IR 第二次抛出的错误没有帮助,但它至少告诉我这是路由器的问题。之后我只是在寻找路线有什么问题。
    • 我现在遇到了一个相关问题,即能够编辑想法。如果你能看,我将不胜感激。 stackoverflow.com/questions/29183247/…
    猜你喜欢
    • 2016-01-16
    • 2015-12-30
    • 2015-02-15
    • 2015-10-16
    • 2014-01-16
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多