[英]How to add Meteor.userId() to SimpleSchema via autoValue?
在我的libs文件夹中,我使用SimpleSchema创建集合。 我想通过autoValue将Meteor.userId添加到某些字段,如下所示:
Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
createdByUser: {
type: String,
max: 20,
autoValue: function() {
return Meteor.userId();
}
}
});
但是,当这样做时,我收到以下错误:
Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
我也尝试过这个:
var userIdentification = Meteor.userId();
Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
createdByUser: {
type: String,
max: 20,
autoValue: function() {
return userIdentification;
}
}
});
这会使我的应用程序崩溃:
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.
有任何想法吗?
userId
信息由collection2
通过this
提供给autoValue
autoValue选项由SimpleSchema包提供,并在那里记录。 对于作为C2数据库操作的一部分调用的任何autoValue函数,Collection2为此添加以下属性:
- isInsert:如果是插入操作,则为True
- isUpdate:如果是更新操作,则为True
- isUpsert:如果是upsert操作(upsert()或upsert:true),则为true
- userId:当前登录用户的ID。 (对于服务器启动的操作,始终为null。)
所以你的代码应该是:
Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
createdByUser: {
type: String,
max: 20,
autoValue: function() {
return this.userId;
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.