2015-07-20 76 views
0

我在我的Meteor項目中使用Autoform包,我試圖加載select元素的選項,如下所示。即使convertToObj函數正確地返回了選擇選項,如下面console.log中所測試的,選擇框沒有填充選項選項,有人可以告訴我我可能在這裏丟失/做錯了什麼嗎?謝謝流星 - 選擇元素選項加載函數總是返回undefined

 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

我可能是錯的,但從我的角度來看,您正在使用異步回調調用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; 
+0

當我將返回結果更改爲return {label:「test」,value:1}時,您是正確的;我仍然沒有在選擇框選項中得到任何東西......任何想法我可能會丟失/如何將生成的值返回到選擇框?謝謝 – MChan

+0

嗨,請閱讀我的更新部分。我想你仍然沒有回到正確的價值觀。應該是一個數組。 –

+0

謝謝,如果我返回您的更新答案中提到的純數組對象,一切工作順利,但如果我使用代碼: 返回Customers.find({_ id:docId})。map(function(c){ convertToObj c.customerOptions,function(result){ console.log(result); //打印確定! 返回結果; });}); 它會一直顯示錯誤。不知道.map調用可能會出現什麼問題?有什麼想法嗎?謝謝 – MChan