Hi everyone,
This post is from StackOverflow, but since the Meteor community doesn’t seem very active there, I repost it here, hoping for answers.
I’ trying to update a value of my user collection, based on the value of a select.
I have no clue what is actually happening.
Here is my relevant code:
// client/settings.html
<template name="settings">
<select id="weightUnit">
<option value="kg" selected="{{selectedWeightUnit 'kg'}}">Kg</option>
<option value="lbs" selected="{{selectedWeightUnit 'lbs'}}" >Lbs</option>
</select>
</template>
// client/settings.js
Template.settings.helpers({
selectedWeightUnit: function(val) {
var weightUnit = Meteor.user().weightUnit;
console.log(weightUnit);
return weightUnit == val;
}
});
Template.settings.events({
"change #weightUnit": function(e) {
e.preventDefault();
var unit = $("#weightUnit").val();
console.log("before: ", Meteor.user().weightUnit);
Meteor.call("updateWeightUnit", unit);
console.log("after: ", Meteor.user().weightUnit);
}
});
Template.settings.onRendered(function () {
$('select').material_select(); // initialize materializecss' select
});
// server/methods.js
Meteor.methods({
updateWeightUnit: function(unit) {
if (this.userId) {
Meteor.users.update( { _id: this.userId }, { $set: { weightUnit: unit }} );
}
}
});
What’s weird is what’s logged in the console:
Screen of console
(sorry I can’t post images directly since I’m a new user)
What I can see and understand:
- At the beginning, weightUnit is
undefined
. (twice since there is 2 options callingselectedWeightUnit
) - When I change the value:
- before I call the update the value is still
undefined
(that’s ok) - after I call the update, the value is
lbs
(the one I chose)
- before I call the update the value is still
What I can see and WTF:
- Why is the page reloading 4 times (8 values, 2 per reload - or whatever it is) ?
- On the first 2 reloads the values stays
lbs
(which is ok) but then changes toundefined
(???!)
Obviously, if I reload (F5) the page, Meteor.user().weighUnit
is undefined
To be honest , this doesn’t make any sense to me.
Could this come from a package I use?
My packages are:
meteor-platform
email
fastclick
meteorhacks:fast-render
iron:router
zimme:active-route
accounts-base
accounts-password
accounts-facebook
accounts-twitter
accounts-google
joshowens:accounts-entry
fourseven:scss
fortawesome:fontawesome
poetic:materialize-scss
aldeed:template-extension
dburles:collection-helpers
meteorhacks:kadira
tap:i18n
u2622:persistent-session