更新扩展 Meteor.user 集合
Update extended Meteor.user collection
我已经扩展 meteor-collection2 用户配置文件
用户个人资料
/// --- User Profile --- ///
UserProfile = new Mongo.Collection("userprofile");
Schema.UserProfile = new SimpleSchema({
userName: {
type: String,
optional: true
},
userGender: {
type: String,
allowedValues: ['Male', 'Female'],
optional: true
},
userAge: {
type: Number,
optional: true,
regEx: /^[0-9]{2}$/
},
userWeight: {
type: Number,
decimal: true,
optional: true
},
userGrowth: {
type: Number,
optional: true
},
userExpirience: {
type: Number,
optional: true
},
userAvatarId: {
type: String,
optional: true
},
profileComplete: {
type: Boolean,
optional: true
}
});
UserProfile.attachSchema(Schema.UserProfile);
用户
/// --- User --- ///
Schema.User = new SimpleSchema({
username: {
type: String,
regEx: /^[a-z0-9A-Z_]{3,15}$/
},
emails: {
type: [Object]
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schema.UserProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
// Add `roles` to your schema if you use the meteor-roles package.
// Option 1: Object type
// If you specify that type as Object, you must also specify the
// `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
// Example:
// Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
// You can't mix and match adding with and without a group since
// you will fail validation in some cases.
roles: {
type: Object,
optional: true,
blackbox: true
},
// Option 2: [String] type
// If you are sure you will never need to use role groups, then
// you can specify [String] as the type
roles: {
type: [String],
optional: true
}
});
Meteor.users.attachSchema(Schema.User);
但我无法更新任何 属性 用户,例如 useAge。
当我尝试这样做时:
Meteor.users.update({_id: userId}, {$set: {userAge: 30}}, {validate: false});
它破坏了用户和集合的数据只包含 id
第二个问题:如何从 ex 年龄的用户配置文件中检索数据?
您覆盖了用户的全部内容,而不仅仅是个人资料中的 userAge。
我认为下面的小改动可以解决您的问题:
Meteor.users.update({_id: userId}, {$set: {profile.userAge: 30}}, {validate: false});
我不确定你所说的"ex age",那个时代的历史是什么意思?
我已经扩展 meteor-collection2 用户配置文件
用户个人资料
/// --- User Profile --- ///
UserProfile = new Mongo.Collection("userprofile");
Schema.UserProfile = new SimpleSchema({
userName: {
type: String,
optional: true
},
userGender: {
type: String,
allowedValues: ['Male', 'Female'],
optional: true
},
userAge: {
type: Number,
optional: true,
regEx: /^[0-9]{2}$/
},
userWeight: {
type: Number,
decimal: true,
optional: true
},
userGrowth: {
type: Number,
optional: true
},
userExpirience: {
type: Number,
optional: true
},
userAvatarId: {
type: String,
optional: true
},
profileComplete: {
type: Boolean,
optional: true
}
});
UserProfile.attachSchema(Schema.UserProfile);
用户
/// --- User --- ///
Schema.User = new SimpleSchema({
username: {
type: String,
regEx: /^[a-z0-9A-Z_]{3,15}$/
},
emails: {
type: [Object]
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schema.UserProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
// Add `roles` to your schema if you use the meteor-roles package.
// Option 1: Object type
// If you specify that type as Object, you must also specify the
// `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
// Example:
// Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
// You can't mix and match adding with and without a group since
// you will fail validation in some cases.
roles: {
type: Object,
optional: true,
blackbox: true
},
// Option 2: [String] type
// If you are sure you will never need to use role groups, then
// you can specify [String] as the type
roles: {
type: [String],
optional: true
}
});
Meteor.users.attachSchema(Schema.User);
但我无法更新任何 属性 用户,例如 useAge。
当我尝试这样做时:
Meteor.users.update({_id: userId}, {$set: {userAge: 30}}, {validate: false});
它破坏了用户和集合的数据只包含 id
第二个问题:如何从 ex 年龄的用户配置文件中检索数据?
您覆盖了用户的全部内容,而不仅仅是个人资料中的 userAge。 我认为下面的小改动可以解决您的问题:
Meteor.users.update({_id: userId}, {$set: {profile.userAge: 30}}, {validate: false});
我不确定你所说的"ex age",那个时代的历史是什么意思?