【问题标题】:Why can't I render this simple list with Meteor?为什么我不能用 Meteor 渲染这个简单的列表?
【发布时间】:2012-08-08 00:19:41
【问题描述】:

这个非常简单的应用程序无法运行。我的列表没有显示。为什么?我一定错过了有关 Meteor 工作原理的重要信息。

recipes.html

<body>
    <h3>Recipes</h3>
    {{> recipes}}
</body>

<template name="recipes">
    <ul>
        {{#each recipes}}
            <li>{{name}}</li>
        {{/each}}
    </ul>
</template>

recipes.coffee

Recipes = new Meteor.Collection("recipes")

if Meteor.is_client
    Meteor.startup ->
        Meteor.autosubscribe(->
            Meteor.subscribe('recipes')
        )

        # THIS IS NOT WORKING
        Template.recipes.recipes ->
            return Recipes.find()

if Meteor.is_server
    Meteor.startup ->
        if Recipes.find().count() == 0
            data = [
               name: "Chocolate Chip Cookies"
            ,
               name: "Spring Rolls"
           ]

        for item in data
            Recipes.insert(item)

    Meteor.publish('recipes', ->
        return Recipes.find()
    )

错误

Uncaught TypeError: Object function (data) {
      var getHtml = function() {
        return raw_func(data, {
          helpers: partial,
          partials: Meteor._partials
        });
      };

      var react_data = { events: (name ? Template[name].events : {}),
                         event_data: data,
                         template_name: name };

      return Meteor.ui.chunk(getHtml, react_data);
    } has no method 'recipes' 

我已经尝试过使用自动发布和不使用自动发布。我在这里不明白什么?

编辑:

正如 Jasd 所指出的,我之前发布了错误的代码。现在的代码就是有问题的代码。

【问题讨论】:

    标签: javascript coffeescript meteor


    【解决方案1】:

    不应该是:

    Template.recipes.recipes = ->
        return Recipes.find()
    

    因为 1.) 您将函数 分配 给 Template.recipes.recipes 和 2.) 您遍历模板 recipes 的列表 recipes。我猜你不需要返回另一个带有键 recipes 的对象。

    【讨论】:

    • 你是对的,但这只是我在发布代码时犯的一个错误。我已经编辑了问题,现在只显示有问题的代码。
    • = 怎么样?我想你需要它。这是 1 中的意思。)
    【解决方案2】:
    1. 您需要确保分配给Template.recipe.recipes(可能是您的 qn 中的拼写错误)

      Template.recipes.recipes = ->
        return Recipes.find()
      
    2. 你不想在 Meteor.startup 中这样做,它在模板被评估之后运行(因此它会认为 Template.recipe.recipes 是 null)——将它移到顶层你的is_client 块,我认为它会没事的。

    【讨论】:

      【解决方案3】:

      应该是这个-

      Recipes = new Meteor.Collection("recipes")
      
      if Meteor.is_client
          Meteor.startup ->
              Meteor.autosubscribe(->
                  Meteor.subscribe('recipes')
              )
      
          # OUTINDENT
          # ASSIGNMEMT, NOT FUNCTION CALL
          Template.recipes.recipes = ->
              return Recipes.find()
      
      if Meteor.is_server
          Meteor.startup ->
              if Recipes.find().count() == 0
                  data = [
                     name: "Chocolate Chip Cookies"
                  ,
                     name: "Spring Rolls"
                  ]
      
                  # INDENT
                  for item in data
                      Recipes.insert(item)
      
          Meteor.publish('recipes', ->
              return Recipes.find()
          )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多