metro.js-在哪里调用对集合的查找

meteor.js - where to call a lookup to a collection

本文关键字:集合 查找 调用 js- 在哪里 metro      更新时间:2024-03-20

所以我有两个集合,'Posts'和'Locations',还有一个简单的html表单,供用户将自己添加到Posts集合中。添加后,我会显示一个所有用户的列表,但如果他们的属性出现在"位置"集合中,我也想向该列表添加一些额外的数据。

因此,例如,用户表单具有Name、Age、Location,Locations集合具有他们居住的位置信息,例如"London,Capital,UK"。如果来自伦敦的用户输入了他们的详细信息,如果他们的位置在"位置"集合中,我希望显示他们在表格上输入的详细信息以及"你住在首都"。

因此,一些代码可以插入表单中的详细信息:

postrongubmit.js:

Template.postSubmit.events({
  'submit form': function(e) {
    e.preventDefault();
    var post = {
      name: $(e.target).find('[id=usersName]').val().toUpperCase();,
      age: $(e.target).find('[id=age]').val(),
      location: $(e.target).find('[id=location]').val().toUpperCase(),
    };
    Meteor.call('postInsert', post, function(error, result) {
      // display the error to the user and abort
      if (error)
        return throwError(error.reason);
      Router.go('postPage', {_id: result._id});  
    });
  }
});

和post.js

Meteor.methods({
  postInsert: function(postAttributes) {
    var post = _.extend(postAttributes, { 
      submitted: new Date(),
    });
    var postId = Posts.insert(post);
    return {
      _id: postId
    };
  }
});

但是,我如何定义对Locations集合的订阅,以及在哪里调用该集合的"查找",以便查找位置,然后使我能够在页面的其他部分中显示该位置。我相信这很简单,不过我很难把这些点联系起来。

您必须制作一个新的发布,发布帖子,然后作为"依赖项"发布位置。有一个包使这件事变得非常简单。copleykj:简单的发布将帮助您实现这一点。自述文件中的示例应该足以让您入门。