Examples for updating users via AutoForm?


#1

Does anyone have any examples on how to update a single field of a user via AutoForm and using Simple Schema?

I currently have a match error when invoking an update method in an AutoForm and I can’t figure out why the validation is failing. This is my full question on SO: http://stackoverflow.com/questions/37646020/what-is-the-correct-way-to-update-a-user-using-autoform-and-simple-schema-in-met

I’ve done it successfully with a completely different collection using the same methodology, so I know it has something to do with Meteor.users being special in a certain way but I haven’t yet been able to find out what is causing the match error.

If you’re reading this and have any code snippets on how to do this or pointers as to what I’m missing that would be awesome, thanks!


#2

See the example of defining Meteor.user schema @ https://github.com/aldeed/meteor-collection2


#3

This {"$set":{"accountBalance":"0"}} should be

{"$set":{"profile.accountBalance":"0"}}


#4

I already have a schema though:

SchemaUsers = new SimpleSchema({
  accountBalance: {
    type: Number,
    label: "Account Balance",
  }
});

Meteor.users.attachSchema(SchemaUsers);

There are other values set and updated when the user logs in, but none that need updating via AutoForm. Only the accountBalance field needs to editable, which is why the schema only has that field.

The accountBalance field isn’t inside of profile, so I’m not sure how this would help. The field is a top-level field because according to http://guide.meteor.com/accounts.html#custom-user-data it’s best not to use profile.


#5

The best way to see the saved structure i would do this

 accountBalance: {
   type: Number,
   label: "Account Balance",
   optional: true,
   autoValue: function() {
            return '400';
        }
   }

This will create a default value 400 and then from there go to mini mongo i.e

mongo mongodb://127.0.0.1:3001/meteor

then

db.users.find().pretty()

then see the actual structure and where accountBalance appears. That way,you can figure out how to update it.


#6

I had not seen your post fully. I think the program is doing exactly what it is supposed to do. Please read autoform docs.

By default, your method will be called with two arguments:
modifier: the modifier object generated from the form values
documentId: the _id of the document being updated

My suggestion is either correct the method (see what doc field contains by displaying it and doing the necessary changes) or use update type using properly defined schema.

You can use profile field as long as you have controls and checks in place.


#7

Thanks for the feedback guys. I checked both:

db.users.find().pretty() showed me the account balance as expected "accountBalance" : 5,

While checking console.log(doc); returned { '$set': { accountBalance: 50 } } which is what it should return when I compare it to the doc structure of the other collection that I also ran a console.log on.

For the time being I have removed the check() as initially only 1 user will have access to updating account balances.

Unfortunately, the doc field contained what was expected, using another update type wouldn’t be ideal as the idea is use a method and the schema is properly defined as far as I can tell.

In conclusion I don’t know how to get to the bottom of this but can work around it for the time being. If anyone can shed more light as to what was missing please feel free.