Meteor - 选择元素选项加载函数始终返回undefined

时间:2015-07-20 16:23:38

标签: javascript meteor meteor-autoform

我在Meteor项目中使用Autoform包,我试图动态加载select元素的选项,如下所示。即使convertToObj函数在下面的console.log中测试正确地返回选择选项,选择框也没有填充选项选项,有人可以告诉我这里可能缺少什么/做错了吗?感谢

      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 {};
                      }
                    }
                }

            }
        }
      },

1 个答案:

答案 0 :(得分:1)

我可能错了,但从我的观点来看,你正在使用异步回调调用convertToObj。因此,如果if (docId)customerRecordCount > 0,您不会返回任何内容(未定义)。

return result的陈述不是options: function()阻止的结果。

对于第一个调试测试,只需return { valid values }并看到选择框填充正确。

更新

您应该返回一个有效的数组,如:

options: [
      {label: "hello", value: 123},
      {label: "world!", value: "world"}
]

options: function() { 
  return [
      {label: "hello", value: 123},
      {label: "world!", value: "world"}
  ]
}

更新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;