简体   繁体   中英

Meteor get Users datas in a schema collection2, and autoform

i want to insert some users datas in a new collection with collection2 and autoform. i have many datas in my Users collection.

I don't know how to do it as the best way ?

Maybe i need to publish and subscribe the Users collection, and then get this datas in my schema.

this is my schema

 Posts = new Mongo.Collection('posts'); PostsIndex = new EasySearch.Index({ collection: Posts, fields: ['title','tags.tags'], engine: new EasySearch.Minimongo() // name: 'PostsIndex', // permission: function(options) { // return userHasAccess(options.userId); // always return true or false here // } }); Posts.allow({ insert: function(userId, doc) { return !!userId; } }); Schema = {}; Schema.Tags = new SimpleSchema({ tags: { type: String } }); Schema.PostsSchema = new SimpleSchema({ title: { type: String, label: '', max: 250, min: 3 }, tags: { type: [Schema.Tags] }, authorId: { type: String, label: 'Author', autoValue: function(){ return this.userId }, autoform: { type: 'hidden' } }, authorName: { type: String, label: 'AuthorName', autoValue: function(){ return this.userId.username }, autoform: { type: 'hidden' } }, createdAt: { type: Date, label: 'CreatedAt', autoValue: function(){ return new Date() }, autoform: { type: 'hidden' } } }); Posts.attachSchema(Schema.PostsSchema);

Thank you in advance :)

this.userId only returns the current user's id and not an object. this.userId.username cannot work. Use this.userId to find a user in the Meteor.users collection and return the username property.

var tempUser = Meteor.users.findOne({_id: this.userId});
return tempUser.username;

From the Meteor documentation :

By default, the current user's username, emails and profile are published to the client.

By default meteor publishes the username, id and emails fields for the documents in the user collection for the accounts system to work. If you want the username of the currently logged in user you can use:

Meteor.user().username;

I just tested this in a client side helper not in an autoValue but I think this should work on the server also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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