2014-03-13 67 views
0

我想從聯繫人中獲取電話號碼的值,但我要麼將[object,Object]返回給我,要麼什麼也沒有。當我嘗試匹配[我] .phoneNumbers我得到[對象,對象],當我添加匹配[i] .phoneNumbers [0] .value,我的警報完全停止。.value返回Nothing(javaScript)

function callme() { 
    var options = new ContactFindOptions(); 
    options.filter = ""; //leaving this empty will find return all contacts 
    options.multiple = true; //return multiple results 
    console.log(options); 
    var filter = ["displayName", "phoneNumbers"]; //an array of fields to compare against the options.filter 

    navigator.contacts.find(filter, successFunc, errFunc, options); 

    function successFunc(matches) { 
     for (var i = 0; i < matches.length; i++) { 
      //this loops through all of the contacts 

      var contact_name = matches[i].displayName; 
      var contact_number = matches[i].phoneNumbers; //this returns [object, Object] 
      // var contact_number = matches[i].phoneNumbers[0].value; returns nothing at all 
      var contact_full = contact_name + " " + contact_number; 
     } 
     alert(contact_full); 
    } 

    function errFunc() { 
     alert("oh no!"); 
    } 
}; 
+0

[學習如何調試JavaScript](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820)並使用'console.log'而不是'alert'。 –

+1

爲什麼不用愚蠢的比賽[我]與console.log(匹配[我]),看看價值是否匹配的屬性 – alexmac

+0

我不能使用console.log,因爲我正在測試這在手機上。 – MissElizabeth

回答

0

我不知道你的JSON對象的結構是,但你可以在Firebug,如果你不知道該怎麼做調試,那麼就嘗試下以下幾點:

function successFunc(matches) { 
     for (var i = 0; i < matches.length; i++) { 
      //this loops through all of the contacts 

     var contact_name = matches[i].displayName; 
     alert(JSON.stringify(matches[i].phoneNumbers); 
var contact_number = matches[i].phoneNumbers; //this returns [object, Object] 
      // var contact_number = matches[i].phoneNumbers[0].value; returns nothing at all 
      var contact_full = contact_name + " " + contact_number; 

    } 
    alert(contact_full); 
} 

警報對話框出現後,您將能夠看到對象的屬性列表。

+0

這是我得到的警報:[{「type」:「mobile」,「value」:「1-555-555-5555」,「id」:「1」,「pref」:false}] – MissElizabeth

+0

試試這個匹配[i] .phoneNumbers [0] [「value」] – user3413046