Search code examples
javascriptmeteormeteor-autoformmeteor-collection2

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 :)


Solution

  • 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.