如何设置一个等于postId [METEOR]的字段

时间:2021-08-01 17:00:30

I have the a posts model that I would like to allow users to "apply" to. Therefore, I have created an applications model using aldeed collections 2 as follows:

我有一个帖子模型,我想让用户“申请”。因此,我使用aldeed集合2创建了一个应用程序模型,如下所示:

applications = new Mongo.Collection('applications');

applications.attachSchema(
    new SimpleSchema({
    post: {
      type: String,
      autoValue: function() {
        if (this.isInsert) {
          return post._id;
        } else if (this.isUpsert) {
          return {$setOnInsert: post._id};
        } else {
          this.unset();
        }
      }
    },
    name: {
      type: String,
      autoValue: function() {
        if (this.isInsert) {
          return Meteor.userId();
        } else if (this.isUpsert) {
          return {$setOnInsert: Meteor.userId()};
        } else {
          this.unset();
        }
      }
    },

    bio: {
      type:String
    }
  })
);

Essentially, I need each application to "belong to" the post that it is being applied to, and the user who has created the application. I figured that the easiest way to do this in Meteor is to set the post value equal to postId. But how do I do this in Meteor? Is there a better way? I am coming from a Rails background.

本质上,我需要每个应用程序“属于”它所应用的帖子,以及创建该应用程序的用户。我认为在Meteor中最简单的方法是将post值设置为postId。但是我如何在Meteor中做到这一点?有没有更好的办法?我来自Rails背景。

P.S. I also use IronRouter.

附:我也使用IronRouter。

1 个解决方案

#1


The code provided seems to be over-complicated, I would prefer to use following Schema:

提供的代码似乎过于复杂,我更愿意使用以下Schema:

 applications.attachSchema(
    new SimpleSchema({
    post: {
      type: String,
    },
    name: {
      type: String,
    },
    bio: {
      type: String
    }
  })
);

And then add new items to the collection with:

然后使用以下内容向集合中添加新项:

var postID = posts.insert({}); // some code that inserts posts

applications.insert({post: postId, name: Meteor.userId(), bio: ''})

Also, as you use a document-oriented database Mongo you may want to combine all the related documents into one document instead of storing IDs of separate collections in application like you would do with a relation-oriented database.

此外,当您使用面向文档的数据库Mongo时,您可能希望将所有相关文档合并到一个文档中,而不是像在面向关系的数据库中那样在应用程序中存储单独集合的ID。

#1


The code provided seems to be over-complicated, I would prefer to use following Schema:

提供的代码似乎过于复杂,我更愿意使用以下Schema:

 applications.attachSchema(
    new SimpleSchema({
    post: {
      type: String,
    },
    name: {
      type: String,
    },
    bio: {
      type: String
    }
  })
);

And then add new items to the collection with:

然后使用以下内容向集合中添加新项:

var postID = posts.insert({}); // some code that inserts posts

applications.insert({post: postId, name: Meteor.userId(), bio: ''})

Also, as you use a document-oriented database Mongo you may want to combine all the related documents into one document instead of storing IDs of separate collections in application like you would do with a relation-oriented database.

此外,当您使用面向文档的数据库Mongo时,您可能希望将所有相关文档合并到一个文档中,而不是像在面向关系的数据库中那样在应用程序中存储单独集合的ID。