简体   繁体   English

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

[英]Method to update users role is giving an error when using Collection2 package

Here is my method: 这是我的方法:

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

Here is the error I receive when looking at the call in Kadira: 以下是我在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)

Line 86 of that methods.js is "Meteor.users.update" in the code above. 该methods.js的第86行是上面代码中的“Meteor.users.update”。 When trying to debug this using breakpoints it appears this is where the error is happening as well. 当尝试使用断点进行调试时,看起来这也是错误发生的地方。

I am using this package to help with the user management UI that I am creating, although I have did some customizing to it. 我正在使用这个包来帮助我创建的用户管理UI,虽然我已经做了一些自定义。 I have also tested this on a different version of my project for troubleshooting and I have found that it works when I don't use the Collection2 package . 我还在我的项目的不同版本上进行了测试以进行故障排除,并且我发现它在我不使用Collection2包时也能正常工作。

Here is my custom schema setup: 这是我的自定义架构设置:

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();
    }
    */
});

To remove a key in a mongodb update you want to use the $unset operator: 要删除mongodb更新中的密钥,您需要使用$ unset运算符:

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

It's just a bit unusual that in your model a user can only have a single role. 在您的模型中,用户只能拥有一个角色,这有点不寻常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用meteorjs中的accounts-password包向用户集合添加collection2架构? - How to add a collection2 schema to users collection using accounts-password package in meteorjs? 使用aldeed collection2的方法上的createuser验证错误 - createuser validation error on method with aldeed collection2 IE9 / 10:Meteor.users.attachSchema-在Meteor.js中使用Collection2的对象不支持属性或方法“ attachSchema” - IE9/10: Meteor.users.attachSchema - Object doesn't support property or method 'attachSchema' using Collection2 in Meteor.js Collection2,使用方法插入,未捕获唯一约束的异常 - Collection2, insert using method, exception from unique constraint not caught 使用$ push时Collection2不会清除数据 - Collection2 doesn't clean data when using $push 流星Collection2更新引发错误 - Meteor Collection2 Update throwing errors 如何添加自己的Meteor.users个人资料; 使用aldeed autoform和collection2 - How to make my own Meteor.users profile additions; using aldeed autoform and collection2 Meteor.js&Collection2:在子模式中插入新对象的更新方法 - Meteor.js & Collection2: Update Method that Inserts New Object in subschema 在Meteor中使用AutoForm和Collection2包的动态Country-State-City - Dynamic Country-State-City using AutoForm and Collection2 package in Meteor Meteor 获取模式集合中的用户数据2,并自动生成 - Meteor get Users datas in a schema collection2, and autoform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM