MeteorJS模板没有显示数据,没有出现 [英] MeteorJS template not showing data, not appearing

查看:121
本文介绍了MeteorJS模板没有显示数据,没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我原本以为我的收藏没有收到数据,但事实证明,我的查询中只有一个错字。但我的数据仍然没有出现在屏幕上。 HTML模板是非常基本的,在这里它是:

 < template name =messagesclass =。messages> ; 
{{#each showMessages}}
< blockquote>
< p> {{message}}< / p>
< / blockquote>
{{/每个}}
< / template>

当我打电话给{{> messages}} $ b时, $ b这里是客户端JS对应的:

  Meteor.subscribe(Messages); 
Template.messages.helpers({
showMessages:function(){
return Meteor.call(find);
}
});

以下是服务器方法:

<$ p $ {
insert:function(username,message){
var id = Messages.insert({$ b $'message':message,
'user':Meteor.userId(),
'username':username,
'timestamp':new Date()
});

返回成功;
},
'find':function(){
return Messages.find({},{sort:{timestamp:-1}},20).fetch ();
}
});

我对MeteorJS非常陌生,昨天刚刚拿起它,所以它可能是非常基本的我错过了,但我一直盯着这个2小时,并取得了0进展。我没有不安全或启用自动发布。这不是一个可用的产品或任何东西,我用它来教我自己,所以我知道我正在做一些不安全的事情。

解决方案

在这种情况下,这主要是对Meteor数据模型的误解。



虽然可以使用方法调用发送数据,但通常您需要使用出版物和订阅将数据发送给客户。这些具有几乎神奇的属性,即查询 - 即查询的任何更新都会自动发送到客户端。当前的代码,如果工作正常,将不会有实时数据。具体问题是 Meteor.call 是异步的,所以你的消息帮助程序不会看到任何东西。



相反,这是你想要做的。在服务器上,您将设置消息集合的发布

  Meteor.publish someWeirdName,function(){
return Messages.find({},{sort:{timestamp:-1},limit:20});
});

请注意与代码的区别:没有 fetch() code>,因为我们想要一个实时游标,并且 20 可能就是你想要的一个限制选项。请注意,我还将 someWordName 称为发布的名称,而不是您将用于在客户端上订阅的集合。有关更详细的解释,您可能需要查看此帖子

然后,在客户端上,您只需要以下内容:

  Meteor.subscribe(someWeirdName); 

Template.messages.helpers({
showMessages:function(){
return Messages.find();
}
});

请注意,您之前调用 Meteor.subscribe(Messages)没有做任何事情,因为没有名为 Messages 的发布。此外,我们将使用消息的客户端缓存创建一个游标来显示消息。



此外,所有这些代码都要求您具有以下声明在服务器和客户端上:

  Messages = new Mongo.Collection(callMeWhateverYouWant); 

请注意,用于实例化此集合的参数与您如何引用集合无关在您的代码中,除非您正在编写自定义发布。它只是标识底层数据库中的集合。


I originally thought my collection wasn't receiving data, but it turns out I just had a typo in my query. But my data still isn't appearing on the screen. The HTML template is pretty basic, here it is:

<template name="messages" class=".messages">
        {{#each showMessages}}
            <blockquote>
                <p>{{message}}</p>
            </blockquote>
        {{/each}}
</template>

It's supposed to just make the collection of messages appear when I call {{> messages}} Here's the client side JS that corresponds to it:

Meteor.subscribe("Messages");
    Template.messages.helpers({
        showMessages: function(){
            return Meteor.call("find");
        }
    });

and here's the server method:

Meteor.methods({
      insert:function(username, message){
          var id = Messages.insert({
              'message': message,
              'user': Meteor.userId(),
              'username': username,
              'timestamp': new Date()
          });

          return "success";
      },
      'find': function(){
            return Messages.find({},{sort:{timestamp:-1}}, 20).fetch();
      }
  });

I'm pretty new to MeteorJS, I just picked it up yesterday, so it's probably something really basic that I am missing, but I've been slamming my head against this for 2 hours and have made 0 progress. I don't have insecure or autopublish enabled. This isn't meant to be a usable product or anything, I'm using this to teach myself, so I know that I am doing some insecure things.

解决方案

In this case, it's mostly about a misunderstanding of Meteor's data model.

Although it's possible to send data using a method call, usually you'll want to use publications and subscriptions to send data to the client. These have the almost magical property that queries are live - that is, any updates to the query will be sent to the client automatically. The current code you have, if it worked properly, wouldn't have live data. The specific issue is that Meteor.call is asynchronous, so your messages helper won't be seeing anything.

Instead, here's what you want to do. On the server, you will set up a publication of the messages collection:

Meteor.publish("someWeirdName", function() {
  return Messages.find({},{ sort: { timestamp:-1}, limit: 20});
});

Note the differences from your code: there is no fetch(), because we want a live cursor, and the 20 is probably what you intended as a limit option. Note that I also called this someWeirdName because that is the name of the publication, and not the collection, which you will use to subscribe on the client. For a more detailed explanation, you may want to see this post.

Then, on the client, you simply need the following:

Meteor.subscribe("someWeirdName");

Template.messages.helpers({
    showMessages: function(){
        return Messages.find();
    }
});

Note that your previous call of Meteor.subscribe("Messages") was doing nothing, because there was no publication named Messages. Also, we're going to use the client side cache of messages to create a cursor to display the messages.

Also, all of this code requires that you have the following declared on both the server and the client:

Messages = new Mongo.Collection("callMeWhateverYouWant");

Note also that the argument used to instantiate this collection has nothing to do with how you refer to the collection in your code, unless you are writing a custom publication. It simply identifies the collection in the underlying database.

这篇关于MeteorJS模板没有显示数据,没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆