Updates of user profile do not work once simple-schema is attached

I’ve been trying to create a userProfile by attaching additional information to Meteor.users using the following packages:

  • aldeed:simple-schema

  • aldeed:Collection2

  • aldeed:AutoForm

The update form generates and populates with the registered email address however I can’t get the submit button to update anything to the database. From the information I’ve read I believe it might be something to do with publish or I need to add a method.

I’m new to programming and meteor could you please be clear and show the code.

My code is below.

Path: collections/UserProfile.js

Schema = {};

Schema.UserProfile = new SimpleSchema({
firstName: {
type: String,
optional: true
},
lastName: {
type: String,
optional: true
},
birthday: {
type: Date,
optional: true
},
gender: {
type: String,
allowedValues: ['Male', 'Female'],
optional: true
}
});

Schema.User = new SimpleSchema({
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,
autoValue: function() {
return new Date()
},
autoform: {
type: "hidden"
}
},
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,
autoform: {
type: "hidden"
}
}
});

Meteor.users.allow({
update: function(userId, doc) {
return !!userId;
}
});

Meteor.users.attachSchema(Schema.User);

Path: client/UserProfile.js

Template.UserProfile.helpers({
user: function(){
return Meteor.user();
},
userSchema: function () {
return Schema.User;
}
});

Path: client/UserProfile.html

<template name="UserProfile">
 {{> quickForm collection="Meteor.users" id="user-profile-form" type="update" doc=user}}
</template>

Path: server/publish.js

Meteor.publish('allUsers', function () {
  return Meteor.users.find();
});

Why is this happening and how can I fix it?

Hi @bp123, super cool that you’re building this!

One way to do it is not actually attach the schema to the users collection. Instead, use a method or method-update autoform type, write a method to do the update, and then in your method validate the schema with Schema.UserProfile.validate(your_document_here).

Let me know if you would like more info on any of those steps!

Tip: It’s easier to read the code if you put it in between three of these backticks ` Check the </> formatting button.

also, check out the autoForm hooks. One of the best ways to figure out why something isn’t processing - is to have a look at it before it goes to the server. It’s typically a mismatch between the schema’s non-optional fields, and what you’re collecting.

PS: throw a AutoForm.debug() in there for some extra verbosity.

Hi @energistic

Thanks your suggestions. I’ve gone back, simplified the code and cleaned it up. Hopefully it is easier to read.
For the purpose of the application I think it will be easier to attach the schema to the users collection. Is there a reason why you suggested I shouldn’t do this?

@romant, thanks for you suggestion. Noob question, where and how do add in AutoForm.debug()

anywhere in the client.

from docs:

While developing, be sure to call AutoForm.debug() in your client code to enable extra logging.

for a start - you can run it in the console of the browser.