0
votes

I'm having some trouble with updating a user account. I use the following schema (collection2):

lib/collections/users.js

Users = Meteor.users;




var Schemas = {};


Schemas.User = new SimpleSchema({
gender: {
    type: Number,

    min: 1

},

s_gender: {
    type: Number,
    min: 1,
    optional:false
},
picture: {
    type: String,
    custom: function() {

        var base64Matcher = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
        var value = this.value.replace("data:image/png;base64,","");
        if(!base64Matcher.test(value))
        {
            return 'no picture';
        }
        else
        {
            return true;
        }

    }
}
});

Users.attachSchema(Schemas.User);

Now I do the update with the following code:

client/templates/start.js

  Users.update({_id: Meteor.userId()}, {
        $set: {picture: picture, gender: gender, s_gender: s_gender}
    }, {validationContext: "updateUser"}, function (error, result) {

        if (error) {
            errorObjs = Users.simpleSchema().namedContext("updateUser").invalidKeys();

            console.log(errorObjs);
        }

        console.log(result);
    });

The validation passes, but I only get a "0" in results (errors are null) - the update isn't working. Errors are shown if I have an empty field, so the validation is working well. If I detach the schema, the update works fine.

Did I forget something here or why isn't he updating when validation passes?

// Edit: Also I see, that Meteor doesn't create users anymore.

1

1 Answers

0
votes

I believe you need to use Users.profile.foo instead of Users.foo, because Users is a special meteor collection and you can only save new fields inside the profile field. Try using Simple Schema/Collection 2 suggested User schema as a starting point (I'll copy it bellow). Notice the "profile schema" is loaded before the user schema:

Schema = {};

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        regEx: /^[a-zA-Z-]{2,25}$/,
        optional: true
    },
    lastName: {
        type: String,
        regEx: /^[a-zA-Z]{2,25}$/,
        optional: true
    },
    birthday: {
        type: Date,
        optional: true
    },
    gender: {
        type: String,
        allowedValues: ['Male', 'Female'],
        optional: true
    },
    organization : {
        type: String,
        regEx: /^[a-z0-9A-z .]{3,30}$/,
        optional: true
    },
    website: {
        type: String,
        regEx: SimpleSchema.RegEx.Url,
        optional: true
    },
    bio: {
        type: String,
        optional: true
    }
});

Schema.User = new SimpleSchema({
    username: {
        type: String,
        regEx: /^[a-z0-9A-Z_]{3,15}$/
    },
    emails: {
        type: [Object],
        optional: true
    },
    "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
    }
});

Meteor.users.attachSchema(Schema.User);

source: https://github.com/aldeed/meteor-collection2