Page 1 of 2

Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Wed Sep 04, 2013 5:54 pm
by Garrett

OK, I have been having trouble with this for a few weeks and am really frustrated...

I have an app setup with the PhoneGap contacts service to import a contact's information (based on a filter [displayname]).

When invoked by a button click, the service returns the contact ($) into a grid.

Inside that grid, I have several other grids... One for the phone numbers, one for the email addresses, and one for the addresses. In each of the grids, there is a text box where the values are displayed.

Using echo in the web preview or compiling in xcode and loading onto an actual device this all works great.

I need to take all of the values for each returned section and put them into one visible text box (the grids mentioned earlier will be hidden). For example... I want each email address in all of the email address grid text boxes to be added together into one visible text box (like "a href="mailto:email1@mail.com" rel="nofollow"email1@mail.com/a, a href="mailto:email2@mail.com" rel="nofollow"email2@mail.com/a, a href="mailto:email3@mail.com" rel="nofollow"email3@mail.com/a"..)

I have been able to get this to work by adding the following javascript to the Email_Input box inside the email grid...
code
var emails, newEmails = '';
emails = Appery('EmailVisible_Input').val();
newEmails = '';

if (value != undefined){
if (phones != ''){
var regex = new RegExp( '\b' + value + '\b' ); //check to see if it is a duplicate entry
if (regex.test(emails) == false){
newEmails = (emails + ", " + value); //at least one addresses already returned, add to the end
} else {
newEmails = emails; //this is the first address returned
}
} else {
newEmails = (type + ": " + value);

}
Appery('EmailVisible_Input').val(newEmails);
}
/code

This works great when there is at least one email address returned by the service.

The problem I have is if no email address is returned (either controlled by modifying the echo response or by importing a contact (that doesn't have an email address) on a phone with a compiled version.

When this happens in the browser preview, the page stops and all further loading halts.
When this happens on an actual phone (xcode compiled and echo turned off), the page freezes and the loading animation keeps spinning indefinitely.

It seems that what ever I try to test for the presence of a value, it freezes if there is not value.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Wed Sep 04, 2013 6:42 pm
by Maryna Brodina

Hello! Working on it.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Wed Sep 04, 2013 7:29 pm
by Maryna Brodina

Code you've posted is invoking on some event? or it's JS from mapping? In code there are "value", "phones", "type" variables, but it's not clear where are they from? Most likely you have error in JS. To find error on device use this utility. It shows device's logs where you need to search for JS error. Some kind of codeD/CordovaLog(11740): Uncaught TypeError: Cannot read property 'name' of undefined/code error.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Wed Sep 04, 2013 7:43 pm
by Garrett

The code is JS from mapping.

"value" is the current data being placed into the text box from the service.

I accidentally posted the wrong code. This is what it should have been...

code
var emails, newEmails = '';
emails = Appery('EmailVisible_Input').val();

if (value != undefined){
if (emails != ''){
var regex = new RegExp( '\b' + value + '\b' ); //check to see if it is a duplicate entry
if (regex.test(emails) == false){
newEmails = (emails + ", " + value); //at least one addresses already returned, add to the end
} else {
newEmails = emails; //this is the first address returned
}
} else {
newEmails = (value);

}
Appery('EmailVisible_Input').val(newEmails);
}

/code

I get no JS error (when using the correct code above).

I am not using android at all right now, so that utility won't help. When I watch the xcode console log for my device, I get no errors either.

And again, using the code above acts completely fine if "value" (emails.value from PhoneGap contact service) has a value.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Thu Sep 05, 2013 7:21 am
by Garrett

In the console, I get "TypeError Cannot read property 'length' of undefined" only when there is no data for a field from the service.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Thu Sep 05, 2013 7:42 am
by Garrett

What is the most efficient way to import a contact's information (from PhoneGap contact service) into a series of text box inputs with the following in mind?

I need all phone numbers listed in one text area in the following format:
code
mobile: 1-800-123-4567
home: 334-3433
/code
(I don't care about the country code if it isn't listed for the contact)

I need all email addresses listed in one text area in the following format:
code
email1@email.com, email2@email.com, email3@email.com
/code

I need all addresses listed in one text area in the following format:
code
123 Main St
Los Angeles, CA 919191
USA

8777 Verdugo Way
Camarillo, CA 93012
USA
/code

Keep in mind that not all contacts will have all info populated.

I require this data to be in this format and editable in my app once loaded into the appropriate text areas.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Thu Sep 05, 2013 1:53 pm
by Anton Artyukh5836028

Can you share app with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a?


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Thu Sep 05, 2013 5:57 pm
by Garrett

I just shared it.

You can try setting the ContactService echo to...
code
[
{
"displayName":"Mark Twain",
"name":{
"formatted":"Mark Twain",
"familyName":"Twain",
"givenName":"",
"middleName":"",
"name":"Mark"
},
"phoneNumbers":[
{
"type":"mobile",
"value":"1234567890",
"pref":"false"
},
{
"type":"home",
"value":"88845452",
"pref":"false"
}
],
"emails":[
{
"type":"custom",
"value":"test01@mail.com",
"pref":"false"
}
]
}

]

/code
and it works. Or you can load it onto a device as a native app and select a contact that has both an email address and a phone number listed.

Change the echo to this, and it doesn't...
code
[
{
"displayName":"Mark Twain",
"name":{
"formatted":"Mark Twain",
"familyName":"Twain",
"givenName":"",
"middleName":"",
"name":"Mark"
},
"phoneNumbers":[
{
"type":"mobile",
"value":"1234567890",
"pref":"false"
},
{
"type":"home",
"value":"88845452",
"pref":"false"
}
]
}

]
/code

Or you can load it onto a device as a native app and select a contact that does not have a phone number listed.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Thu Sep 05, 2013 8:34 pm
by Maryna Brodina

Hi, thanks. We'll take a look.


Trouble with PhoneGap Contact service. If a field is missing or blank, the code stops.

Posted: Fri Sep 06, 2013 12:51 pm
by Anton Artyukh5836028

Hi Garrett,

Sorry for the delay.

Try to use Success Event without mapping. You can access to response by using arguments[0] or data variable.

Mapping expect full structure for every response. But different devices can return only non empty parts of structure (as your echo example). So you need manual control for data integrity.