I am creating a personal CRM using the Expo CLI. I'm trying to import the contacts from a users phone into the database and then display those imported contacts.
Currently, it only returns a partial subset of the contacts.
It displays those contacts correctly but it doesn't seem to register the vast majority of contacts when testing on my personal phone, as well as other test phone cases.
I'm using SQLLite as a DB (just to make the code understandable to read!)
I'm just getting the name, first phone number and first email if they exist and sending that to the database. A different function then renders all the contacts from the database.
useEffect(() => {
(async () => {
if (!ImportContacts.isAvailableAsync()) {
if (ImportContacts.getPermissionsAsync()) {
Alert.alert('If you choose to import contacts, only the name, first phone number, and email will be added.')
}
}
const { status } = await ImportContacts.requestPermissionsAsync();
if (status === 'granted') {
const { data } = await ImportContacts.getContactsAsync({
fields: [ImportContacts.Fields.Emails, ImportContacts.Fields.PhoneNumbers],
});
db.transaction(txn => {
txn.executeSql(
`CREATE TABLE IF NOT EXISTS cards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(20),
phone VARCHAR(20),
email VARCHAR(35),
dateAdded TEXT,
isAce INTEGER,
image VARCHAR(500)
)`,
[],
(sqlTxn, res) => {
console.log('table card created');
},
error => {
console.log('error on creating table CARD 123')
},
);
});
for (let i = 0; i < data.length; i++) {
console.log(data[i]);
let name = "";
let phone = "";
let email = "";
if (data[i].name){
name = data[i].name;
}
if (data[i].phoneNumbers){
phone = data[i].phoneNumbers[0].number;
}
if (data[i].emails){
email= data[i].emails[0].email;
}
db.transaction(txn => {
//console.log(moment().format("MMM Do YY"));
txn.executeSql(
'INSERT INTO cards (name, phone, email, dateAdded, isAce) VALUES (?,?,?,?,?)',
[name, phone, email, moment().format("MMM Do YY"), 0],
(sqlTxn, res) => {
},
error => {},
);
});
}
}
else {
}
})();
}, []);