简体   繁体   English

使用Meteor JS的反应式联接在模板中呈现收集数据

[英]Rendering collection data in a template using a reactive join with Meteor JS

I would like to output data from two collections using a reactive join into my template, then pair the users, post and comments through a common id. 我想使用反应连接将两个集合中的数据输出到我的模板中,然后通过通用ID将用户配对,发布和评论。

So far, I can see with Mongo commands that the JSON data exist, but my template doesn't render any data. 到目前为止,我可以通过Mongo命令看到JSON数据存在,但是我的模板没有呈现任何数据。 What am I doing wrong? 我究竟做错了什么?

FYI, the meteorpad doesn't compile but the github repo will. 仅供参考,meteorpad不会编译,但是github存储库会编译。

Repo: 回购:

https://github.com/djfrsn/frontend-interview-test-long/tree/master/ontra/ontra https://github.com/djfrsn/frontend-interview-test-long/tree/master/ontra/ontra

Meteor Pad Example: 流星垫示例:

http://meteorpad.com/pad/SvwkNrv5grgv2uXxH/Copy%20of%20Leaderboard http://meteorpad.com/pad/SvwkNrv5grgv2uXxH/Copy%20of%20Leaderboard

Your code doesn't run on meteorpad because the fetchJSONData method is executed on the server before it is defined in the common.js file. 您的代码无法在meteorpad上运行,因为fetchJSONData方法是在common.js文件中定义之前在服务器上执行的。 You should probably be calling the method after an event triggered on the client, or not use a method at all and simply fetch your JSON data on Meteor.startup. 您可能应该在客户端触发事件之后调用该方法,或者根本不使用任何方法,而只是在Meteor.startup上获取JSON数据。

Regarding the reactive join, it seems you want to do something very similar to Example 1 of the documentation: https://github.com/englue/meteor-publish-composite 关于反应式连接,看来您想做与文档示例1非常相似的操作: https : //github.com/englue/meteor-publish-composite

There's so much wrong that it's hard to know where to start. 有太多的错误,很难知道从哪里开始。

1) When you're loading the initial post and user data you're inserting the whole returned array as one element rather than inserting each element individually into your posts collection. 1)在加载初始帖子和用户数据时,您会将整个返回的数组作为一个元素插入,而不是将每个元素单独插入您的posts集合中。

2) You're creating a publish subscription with the name "postsSet", but you're trying to subscribe to it with a different name. 2)您正在创建名称为“ postsSet”的发布订阅,但是您尝试使用其他名称进行订阅。

3) You're not calling publishComposite correctly at all. 3)您根本没有正确调用publishComposite。 You should be publishing the user required for each post as part of the children array. 您应该将每个帖子所需的用户发布为children数组的一部分。

4) The template needs updating based on the above 4)模板需要根据以上内容进行更新

5) The username needs to be supplied via a helper. 5)用户名需要通过助手来提供。

6) You should really map the "id" attributes to Mongo's "_id" instead. 6)您应该真正将“ id”属性映射到Mongo的“ _id”。

Here's come code which works. 这是有效的代码。 Note that you'll need to call meteor reset everytime you restart, otherwise you'll get duplicate id errors since you currently reimport the data every time. 请注意,您每次重新启动时都需要调用meteor reset ,否则会由于当前每次重新导入数据而收到重复的id错误。

Posts = new Mongo.Collection("Posts");
var groundPosts = new Ground.Collection(Posts);
Users = new Mongo.Collection("Users");
var groundUsers = new Ground.Collection(Users);

if (Meteor.isClient) {

  Meteor.subscribe("postsSet");

  console.log('POSTS DATA = ' + Posts.find().fetch());
  console.log('USERS DATA = ' + Users.find().fetch());

  Template.body.events({
    "submit .ontra": function (event) {    
    // This function is called when the new task form is submitted

    var text = event.target.text.value;

    Posts.insert({
      content: text,
      date: new Date() // current time
    });

    // Clear Form
    event.target.text.value = "";

    // Prevent default form submit
    return false
    }
  });

  Template.body.helpers({
    posts: function() {
      return Posts.find();
    },
  });

  Template.post.helpers({
    username: function() {
      return Users.findOne({_id: this.userId}).username;
    }
  });

}



Meteor.methods({
  'fetchJSONData': function() {

    var postsResponse = Meteor.http.call("GET","https://raw.githubusercontent.com/djfrsn/frontend-interview-test-long/master/codetest/data/posts.json");
    var usersResponse = Meteor.http.call("GET","https://raw.githubusercontent.com/djfrsn/frontend-interview-test-long/master/codetest/data/users.json");

    var postsData = JSON.parse(postsResponse.content);

    var usersData = JSON.parse(usersResponse.content);

    postsData.forEach(function (post) {

      post.date = new Date();
      post._id = String(post.id)
      delete post.id
      post.userId = String(post.userId)
      Posts.insert(post);
    });

    usersData.forEach(function (user) {
      user.date = new Date() // current time
      user._id = String(user.id)
      delete user.id
      Users.insert(user);
    });


  }
});




if (Meteor.isServer) {

  Meteor.publishComposite('postsSet', {
    find: function () {
      return Posts.find({});
    },
    children: [
      {
        find: function (post) {
          console.log("%j", post.userId);
          console.log("%j", Users.findOne({ _id: post.userId }));
          return Users.find({ _id: post.userId });
        }
      }
    ]
  });

  Meteor.call("fetchJSONData");
  //console.log('POSTS DATA = %j', Posts.find().fetch());
  //console.log('USERS DATA = %j', Users.find().fetch());

}

HTML: HTML:

<head>
  <title>ontra</title>
</head>

<body>

  <div class='container'>

    <header>
      <h1>ontra</h1>
            <form class='ontra'>
                <input type='text' name='text' placeholder="Type to add new post">
            </form>
    </header>
    <ul>
      {{#each posts}}
        {{> post}}
      {{/each}}
    </ul>

  </div>

</body>

<template name='post'>

  <li>
    <span class="text">{{content}}</span>
    <span class="text">{{username}}</span>
  </li> 

</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM