更新用户角色的方法在使用Collection2包时出错

时间:2016-01-26 03:12:59

标签: meteor

这是我的方法:

removeRole: function(role) {
check(role, String);
    var user = Meteor.user();
if (!user || ! AccountsAdmin.checkForAdminAuthentication(user))
  throw new Meteor.Error(401, "You need to be an authenticated admin");

    // handle non-existing role
    if (Meteor.roles.find({name: role}).count() < 1 )
        throw new Meteor.Error(422, 'Role ' + role + ' does not exist.');

    if (role === 'admin')
        throw new Meteor.Error(422, 'Cannot delete role admin');

    // remove the role from all users who currently have the role
    // if successfull remove the role
    Meteor.users.update(
        {roles: role },
        {$pull: {roles: role }},
        {multi: true},
        function(error) {
            if (error) {
                throw new Meteor.Error(422, error);
            } else {
                Roles.deleteRole(role);
            }
        }
    );
},

以下是我在Kadira中查看电话时收到的错误:

message: After filtering out keys not in the schema, your modifier is now empty 
stack:
Error: After filtering out keys not in the schema, your modifier is now empty
    at [object Object].doValidate (packages/aldeed_collection2-core/lib/collection2.js:282:1)
    at [object Object]._.each.Mongo.Collection.(anonymous function) [as update] (packages/aldeed_collection2-core/lib/collection2.js:83:1)
    at [object Object].Meteor.methods.removeRole (packages/accounts-admin-ui-bootstrap-3/server/methods.js:86:1)

那些methods.js的第86行是&#34; Meteor.users.update&#34;在上面的代码中。当尝试使用断点进行调试时,看起来这也是发生错误的地方。

我正在使用this package来帮助我创建的用户管理界面,尽管我已经对其进行了一些自定义操作。我还在我的项目的不同版本上对此进行了测试以进行故障排除,并且我发现它在我不使用Collection2 package时有效。

这是我的自定义架构设置:

Schema = {};

Schema.UserProfile = new SimpleSchema({
    userProfile: {
        type: Object
    },
    'userProfile.firstName': {
        type: String,
        optional: true,
        label: "First Name"
    },
    'userProfile.lastName': {
        type: String,
        optional: true,
        label: "Last Name"
    },
    'userProfile.birthday': {
        type: Date,
        optional: true,
        label: "Date of Birth"
    },
    'userProfile.contactEmail': {
        type: String,
        optional: true,
        label: "Email"
    },     
    'userProfile.gender': {
        type: String,
        allowedValues: ['Male', 'Female'],
        optional: true,
        label: "Gender"
    },
    'userProfile.address': {
        type: String,
        optional: true,
        label: "Address"
    },
    'userProfile.city': {
        type: String,
        optional: true,
        label: "City"
    },
    'userProfile.stateProvince': {
        type: String,
        optional: true,
        label: "State/Province"
    },
    'userProfile.postalCode': {
        type: String,
        optional: true,
        label: "Postal Code"
    },
    'userProfile.phoneNumber': {
        type: String,
        optional: true,
        label: "Phone Number"
    },
    userProfilePayment: {
        type: Object
    },
    'userProfilePayment.paymentEmail': {
        type: String,
        optional: true,
        label: "Payment Email"
    },
    'userProfilePayment.address': {
        type: String,
        optional: true,
        label: "Address"
    },
    'userProfilePayment.city': {
        type: String,
        optional: true,
        label: "City"
    },
    'userProfilePayment.stateProvince': {
        type: String,
        optional: true,
        label: "State/Province"
    },
    'userProfilePayment.postalCode': {
        type: String,
        optional: true,
        label: "Postal Code"
    },
    'userProfilePayment.phoneNumber': {
        type: String,
        optional: true,
        label: "Phone Number"
    },

});

Schema.User = new SimpleSchema({
    username: {
        type: String,
        // For accounts-password, either emails or username is required, but not both. It is OK to make this
        // optional here because the accounts-password package does its own validation.
        // Third-party login packages may not require either. Adjust this schema as necessary for your usage.
        optional: true
    },
    emails: {
        type: Array,
        // For accounts-password, either emails or username is required, but not both. It is OK to make this
        // optional here because the accounts-password package does its own validation.
        // Third-party login packages may not require either. Adjust this schema as necessary for your usage.
        optional: true
    },
    "emails.$": {
        type: Object
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date
    },
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    // Make sure this services field is in your schema if you're using any of the accounts packages
    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
    },
    // In order to avoid an 'Exception in setInterval callback' from Meteor
    heartbeat: {
        type: Date,
        optional: true
    },
    // Added to work with mizzao:user-status
    status: {
        type: Object,
        optional: true,
        blackbox: true
    }
});

Meteor.users.attachSchema(Schema.User);

Meteor.users.allow({
    // NOTE: The client should not be allowed to add users directly!
    insert: function(userId, doc) {
        // only allow posting if you are logged in
        console.log("doc: " + doc + " userId: " + userId);
        return !! userId;
    },

    update: function(userId, doc, fieldNames) {
        // only allow updating if you are logged in
        console.log("doc: " + doc + " userId: " + userId);
        // NOTE: a user can only update his own user doc and only the 'userProfile' and 'userProfilePayment' field
        return !! userId && userId === doc._id && _.isEmpty(_.difference(fieldNames, ['userProfile, userProfilePayment'])); 
    },
    /* NOTE: The client should not generally be able to remove users
    remove: function(userID, doc) {
        //only allow deleting if you are owner
        return doc.submittedById === Meteor.userId();
    }
    */
});

1 个答案:

答案 0 :(得分:0)

要删除mongodb更新中的密钥,您要使用$unset运算符:

Meteor.users.update({ roles: role },{ $unset: { roles: 1 }}, { multi: true })

在您的模型中,用户只能拥有一个角色,这有点不寻常。