0

I'm trying to access the mobile contacts using phone gap on Android. I access the contacts & show them in a drop down box, then the user should select the desired contact and this contact is supposed to be shown at a text box after that.

Now when I click on one of the contacts in the contact list, the text box's value changes to [object object].

I tried:

    JSON.stringify(data.value);

but it just changes [object object] to "[object object]".

I tried:

    data.value.tostring(); 

&

    data.value.formatted;

but then the output is undefined.

That's the jS line I change the text box content with:

       document.getElementById ("friendName").value = "/*Stringified data is written here*/;

So do you know how can I change the object object, to a normal readable string ?

2
  • what does the JSON look like? Commented Apr 26, 2012 at 13:00
  • Here it is : $('#contacts').append('<option class="BigDiv" value="'+contacts[i].name+'" >' + contacts[i].name.formatted + '</select>'); Commented May 1, 2012 at 11:28

2 Answers 2

2

If your "data" object is of type Contact then you should be using data.displayName or data.name.formatted if you want the contacts name.

Sign up to request clarification or add additional context in comments.

5 Comments

But when I write: selectedArray[count] = selContacts.options[i].name.formatted; It can't recognize the word formatted, do you know How the syntax of the line should be written ?
I need to see more of your code to answer without any certainty. How did you get the array of contacts? What does your find method look like and what does your success callback look like.
If you need to know any more info, please tell me. Thanks.
You are setting the value of your option box to the contact.name object. Try asking for selectedArray[count] = selObj.options[i].value.formatted as that should get you the formatted name from the object you set as value.
I just realized that you are trying to set "value" with an Object. What is actually happening is that you are coercing the Object into a String so the actual string that is beting set to "value" is "[Object object]". So you'll need to re-think how you want to accomplish your task.
1

It looks like the contact object you are working with does not have a toString() implementation; the default JavaScript toString() will give you the results you are getting. You will have to write a function that pulls the member values you want and creates a string. Or, you could add a toString(), but I don't think that is the best option because the Contact object appears to be from a 3rd party, Cordova, as identified in the answer by @Simon MacDonald.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.