2015-11-17 33 views
0

我有我在流星的應用程序調用的方法和它的接受可以根據變化其中呼叫是來自參數的個數:如何接受Mongo查詢中的條件參數?

myFunc: function (attr){ 
    if(typeof attr === 'object') { 
     return Items.update(
       { 
        _id: attr.itemId 
       }, 
       { 
        $set: { 
         field_1: attr.someValue1, 
         field_2: attr.someValue2 
        } 
       }, 
       function (error, result) {}); 
    } 
} 

在這個例子中,「attr.someValue2」可以或者可以不在傳遞給此函數的attr對象中,那麼構造上述查詢的最佳方法是什麼?

回答

0

您需要的時候在對象參數不提供的屬性來提供默認值:

myFunc: function (attr){ 
    if(typeof attr === 'object') { 
    return Items.update(
     { 
     _id: attr.itemId 
     }, 
     { 
     $set: { 
      field_1: attr.someValue1 === undefined ? attr.someValue1 : 'N/A', 
      field_2: attr.someValue2 === undefined ? attr.someValue2 : 'N/A' 
     } 
     }, 
     function (error, result) {} 
    ); 
    } 
} 
+0

如果someValue1是不確定的,將field_1保留在在那裏已經數據庫中的值? – JoeTidee