PhoneGap contact fetching problem

  • 1
  • Question
  • Updated 4 years ago
  • Doesn't Need an Answer
I'm facing a serious issue while fetching contacts from the contacts list using PhoneGap. I have installed the plugin and did everything but seems it doesn't work for me.

So, What is the problem ? The problem with it is my contacts having the email accounts.So email doesn't have the phonenumber.
Here is the screenshot : http://prntscr.com/82meyr

What is the issue ?
Well, the problem is that when it encounters the email the for loop stops immediately and shows the 20-30 contacts only!

function onSuccess(contacts)
{
for (var i=0; i);
}
}

My code :
http://stackoverflow.com/questions/31...

Is there any way to bypass the email accounts ?
Thank you!
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes

Posted 5 years ago

  • 1
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
You are accessing phonenumbers[0] on a null value. That's what makes the loop end.
Simply check for null-value of phonenumbers (NOT an empty array!). If null, then don't look for phonenumbers[0].
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes

function onSuccess(contacts) {

alert("Total contacts = "+ contacts.length);

for (var i=0; i<contacts.length; i++)
{

if(contacts[i].phoneNumbers[0].value !== null)
{
// console.log("Display Name = " + contacts[i].phoneNumbers[0].value);
$('#contactList').append("<li><a href='#'><h2>"+ contacts[i].displayName +"</h2><p>" + contacts[i].phoneNumbers[0].value + "</p></a></li>");

}
$('#contactList').listview("refresh");

}

}

Still same error!
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Because you are still using phonenumbers[0], which is undefined!
You can not evaluate phonenumbers[0], because it does not exist.

Why don't you just display the full contents of the contacts object for test purposes? You would immediately see what you got returned, and how to loop through the object.
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
How do I do it ? Please help :)
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
I want to loop through each contact number and then I want to check if it exists on my database(mysql) using http request. If the number exists then I will store it on SQLITE db.
Is that correct ?

function onSuccess(contacts) {

alert("Total contacts = "+ contacts.length);

for (var i=0; i<contacts.length; i++)
{

for(var j=0; j<contacts[i].phoneNumbers.length;j++)
{
$('#contactList').append("<li><a href='#'><h2>"+ contacts[i].phoneNumbers[j].value + "</h2></a></li>");
}

}
$('#contactList').listview("refresh");
}
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
I could. But I'd like to understand the case better. That's why I have a couple of questions, first:
- do you only want to store phone numbers, or full contacts records, including all other fields?
- how and when do you want to retrieve these data?
- how critical is synchronization with your remote database?
- did you display the full contacts object for test purposes? Have you corrected the errors above?
- is this a homework assignment?
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
Homework assignment ? LOL man you are kidding me. Here people doesn't even know about what phonegap is. It's part of my learning. I just moved from android development. I asked my professors but they doesn't even know what is phonegap or apache cordova.
Anyway, What I want to do is, I want to get the contact number(mobile) and want to check on the mysql database using POST method. If that exists then it will show all the details of the database and it will store it in the local database. Contacts name and phone number will be stored from the local contact list, rest will be stored from the JSON result.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
So, are you going to delete everything and rebuild this local database every time the user gets to the 'front page' of your app? Every xx minutes? On user request?

I ask, because redundant database synchronization is a tough task to get right. Perhaps other options, like retrieving remote data just in time when needed, would be a better fit. You would then not have anything redundant, and the displayed data would always be current values.

If, however, this is just an exercise for you, I would think that you learn most by exploring different application options yourself, then research the plugin documentation.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
BTW: shutting down PC now. It's getting late, here.
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
That will be for the first time. If the user exists in the database then it will store it in the table user_exists and store the information if the user doesn't exists then it will be stored in the another table called no_user. So if I want to update the existing user then I will use user_exists phonenumbers to update the local database. And I will have another background thread which will check no_user table. If from the no_user table any user got registered then that row will be deleted and will be added on user_exists table. That's it.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
1. How will you know who the user is? Will (s)he have to log in, first?

2. Since you are storing data locally, what if the user has multiple devices? What if the contacts of these devices are not identical?
Can your UserABC have two devices with different Google accounts, different contacts and still be one user in your database?

3. Your two tables seem utterly redundant. Isn't the fact that a user exists merely an attrbute of the entity 'user'? Once a user exists, you should just set that attribute to true.

4. Suppose you delete a phonenumber from the nysql database, how and when would it be deleted from the device?
(Edited)
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
The best example would be "whatsapp". I don't want to make messenger but something like that. Instead of chats I just want to show the user's information from MySQL. A user can have two or more devices and app will able to run. I used some logic and wrote the following code but it gives the duplicate phoneNumbers. Here is the code :

function onSuccess(contacts)
{

for(var i=0;i<contacts.length;i++)
{
if(contacts[i].phoneNumbers)
{
for (var j = 0; j < contacts[i].phoneNumbers.length; j++)
{
$('#contactList').append("<li><a href='#'><h2>"+ contacts[i].displayName +"</h2><p>" + contacts[i].phoneNumbers[0].value + "</p></a></li>");

}
}
}

Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
You're writing phonenumbers[0] every time.

Have you displayed the full contacts object for test purposes now, so you can actually SEE what you're doing?
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
Yes. I used console.log but didn't help.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Sorry, I don't have Whatsapp, so I can't see how that answers the above questions.
And even if I had it, I could probably not see its application architecture or data model.
Can you?
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
Let me explain.Let's I have some registered users in my database. Now If I want to register then I will install the App. At the first time user will enter his phone number and app will check if the user exists or not if the user exists then it will redirect to the login page and if the user doesn't exists then app will send the verification code on his/her device. If the code matches then the App will collect the local contacts. Then it will send the phone number list and get the JSON output of the registered user.
That's it.
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
But if user wants to see the data offline without internet connection then SQLITE will let me to do this.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Without data connection, would it be better to show him unreliable data (they may have changed long ago) or no data at all?
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
Yes I want to show.
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Petra
I see at least 6 posts for this request (in 24 hours), between stackoverflow and here (2). I suspect all the same OP.

@Abhishek,
Working code that compiles on Phonegap Build
https://github.com/jessemonroy650/Phonegap-Contact-Test

Jesse
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Yes, (s)he asked twice here. I simply ignored one of them.
Since I don't follow any stackoverflow threads, I didn't know about the others.

I wonder why the OP would do such a thing. It's not of his advantage, is it?
Abhishek, can you clarify, please?
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
@Petra. Instead of asking too many questions you could have given the solution. It's better to do by myself. Thank you.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
I don't have solutions for undefined problems, that's why I need to ask, first.
However, if this thread has helped you to understand the implications of your requirements, that's a good result.

And surely, if your aim was to learn, it's way better to do it by yourself.
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
Undefined ? I clearly explained that I want to collect the phonenumber list and want to send it. I also posted the code. And I also said that it's duplicating the code. But you are going into deep.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Do you really want this argument? Fine.
Then please show me where you said "it's duplicating code".

(The only thing about 'duplicating' I can find is about the same phone number multiple times, and I gave you the cause for that immediately: it was your javascript error, where you hard-coded the display of phoneNumbers[0].)
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
Duplicating numbers*
Photo of Bhumika Patel

Bhumika Patel

  • 2 Posts
  • 0 Reply Likes
i found invalid date issue check my website post.
http://phonegapcmsworld.blogspot.in/2...
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
Your blog a has a very large image. Are you lazy or inconsiderate of the time other developer spend helping you? Fix your blog.
Photo of Bhumika Patel

Bhumika Patel

  • 2 Posts
  • 0 Reply Likes
Yes, it may have large images but my blog also have the solution for the issues.