Reputation: 7162
I am using Autoform package in my Meteor project where I am trying to load the options of a select element dynamically as shown below. Even though the convertToObj function returns the select options correctly as tested in console.log below, the select box is not populated with the options options, can someone please tell me what I might be missing / doing wrong here? Thanks
var convertToObj = function(arrayObj, callbackFn){
var customerOptions = [];
arrayObj.forEach(function(optionName){
customerOptions.push( {label: optionName.name, value: optionName.value} );
});
callbackFn(customerOptions);
}
customerOpt:{
type: String,
label: "Customer Options",
optional: true,
autoform: {
type: "select",
options: function(){
if (Meteor.isClient) {
var docId = '';
docId = AutoForm.getFieldValue('customer');
if(docId){
var customerRecordCount = Customers.find({_id:docId, customerOptions:{$exists:true}}).count();
if( customerRecordCount > 0){
Customers.find({_id: docId}).map(function (c) {
convertToObj(c.customerOptions, function(result){
console.log(result); //Prints OK!
return result;
});
});
}else{
return {};
}
}
}
}
}
},
Upvotes: 0
Views: 120
Reputation: 1367
I may be wrong but from my point of view you are calling the convertToObj with an Async callback. So you do not return anything (undefined) in case of if (docId)
and customerRecordCount > 0
.
The statement of return result
is NOT the result for your options: function()
block.
For a first debugging test, just return { valid values }
and see that the select box is filled quite right.
Update:
You should return a valid array like:
options: [
{label: "hello", value: 123},
{label: "world!", value: "world"}
]
or
options: function() {
return [
{label: "hello", value: 123},
{label: "world!", value: "world"}
]
}
Update 2:
var selectOptions = []
var customerRecord = Customers.findOne({_id:docId})
if (customerRecord) {
_.each(customerRecord.customerOptions, function(opt) {
selectOptions.push({label: opt.name, value: opt.value});
})
}
return selectOptions;
Upvotes: 1