1
我有我的應用程序中使用cordova contacts plugin用戶在線數據庫檢查用戶聯繫人的功能。如果用戶設備中的任何聯繫人都有帳戶,則該設備上的帳戶數據會更新。科爾多瓦聯繫人插件聯繫人數據複製,而不是覆蓋聯繫人數據
該功能適用於諸如姓名和出生日期之類的內容,但電子郵件和電話號碼不會覆蓋現有的聯繫人數據,而是爲這些數據創建新條目。
這個特定的用戶應該只有1個電子郵件地址和1個電話號碼。
JSON作出反應並保存
var contacts = json.contacts;
for(var x = 0; x < contacts.length; x++){
var user = contacts[x];
//console.log(user);
//get old contact by id
//Set the options for finding conact
var options = new ContactFindOptions();
options.filter = user.id; //name that you want to search
options.multiple = false;
var fields = ["id","displayName"];
navigator.contacts.find(fields, function(c){
c = c[0];
var dob = user.iceTV.dob.split(':');
c.displayName = user.iceTV.f_name + " " + user.iceTV.l_name;
c.nickname = user.iceTV.f_name + " " + user.iceTV.l_name;
c.birthday = new Date(dob[2], dob[1] - 1, dob[0]);
c.name.givenName = user.iceTV.f_name;
c.name.familyName = user.iceTV.l_name;
c.name.formatted = user.iceTV.f_name + " " + user.iceTV.l_name;
if(c.emails == null){
c.emails = [];
}
for(var a = 0; a < user.emails; a++){
c.emails[a] = { value : user.emails[a] };
}
if(c.phoneNumbers == null){
c.phoneNumbers = [];
}
c.phoneNumbers[0] = {type : 'mobile', value: user.iceTV.number};
if(user.pp == null){
c.photos = null;
} else {
contacts.photos[0] = {
pref : false,
type : 'url',
value : user.iceTV.pp
};
}
c.save(function(saveSuccess) {
console.log('User updated' ,c);
}, function(saveError){
console.log("Error when updating");
});
}, function (e){
makeToast("Error = " + e.code);
}, options);
}