简体   繁体   中英

Meteor afFormGroup not populating dropdown box

The html is as follows:

<template name="addUser">
  {{#autoForm id="addUser" schema=schema meteormethod="newUser"}}
  <fieldset>
        {{> afQuickField name="fName"}}
        {{> afQuickField name="lName"}}
        {{> afQuickField name="username"}}
        {{> afQuickField name="email"}}
        {{> afFormGroup name="roles" firstoption="Select Role"    options=addUseroptions type="select"}}
        {{> afQuickField name="password"}}
    <div>
      <button type="submit" class="btn btn-primary">Submit</button>
      <button type="reset" class="btn btn-default">Reset</button>
    </div>
  </fieldset>
  {{/autoForm}}
</template>

The afFormGroup roles is not populating. I am attempting to populate a dropdown box with all of the roles currently stored in the mongodb, but the dropdown remains empty. The correct values are shown in the console from the console.log() statements.

The js is as follows:

Template.addUser.helpers({
    schema : function (){
        return MySchema.addUser;
    },
    collection : function (){
        return MyCollections.Users;
    },
    addUseroptions: function (){
      var array = [];
      var array2 = [];
      roles = Roles.getAllRoles();
      array = roles.fetch(); 

      for(var i = 0; i < roles.count(); i++){
      array2[i] = array[i].name;
      }
      console.log(roles.count());
      console.log(array2);

      return
      [{
        optgroup: "Roles",
        options: array2
      }];
    }
  });

Your help function seems a bit confused. Try this:

// Still inside your "helper" block
addUseroptions: function() {
  return Roles.getAllRoles().map(function(r) {
    // "r" here is a single document returned by the cursor. Use this
    // space to do whatever change you wanna do in respect to
    // label name or select value
    return {label: r.name, value: r.name};
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM