【问题标题】:Meteor get Users datas in a schema collection2, and autoformMeteor 在模式集合中获取用户数据2,并自动生成
【发布时间】:2015-11-17 19:03:34
【问题描述】:

我想使用 collection2 和 autoform 在新集合中插入一些用户数据。我的用户集合中有很多数据。

我不知道怎么做是最好的方法?

也许我需要发布和订阅用户集合,然后在我的架构中获取这些数据。

这是我的架构

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

提前谢谢你:)

【问题讨论】:

    标签: javascript meteor meteor-autoform meteor-collection2


    【解决方案1】:

    this.userId 只返回当前用户的 id 而不是一个对象。 this.userId.username 无法工作。使用this.userId 在 Meteor.users 集合中查找用户并返回用户名属性。

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

    来自流星documentation

    默认情况下,当前用户的用户名、电子邮件和个人资料都会发布到客户端。

    【讨论】:

      【解决方案2】:

      默认情况下,meteor 会为用户集合中的文档发布用户名、id 和电子邮件字段,以便帐户系统正常工作。如果你想要当前登录用户的用户名,你可以使用:

      Meteor.user().username;
      

      我刚刚在客户端助手中测试了这个,而不是在 autoValue 中,但我认为这也应该在服务器上工作。

      【讨论】:

        猜你喜欢
        • 2015-05-22
        • 2022-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        • 1970-01-01
        • 2015-05-10
        • 2020-10-29
        相关资源
        最近更新 更多