cordova联系人插件联系人数据正在复制,而不是覆盖联系人数据 [英] cordova contacts plugin contact data is duplicating instead of overwriting contact data

查看:151
本文介绍了cordova联系人插件联系人数据正在复制,而不是覆盖联系人数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中有功能,可以使用



此特定用户应该只有1个电子邮件地址和1个电话号码。



JSON回应并储存

  var contacts = json.contacts; 
for(var x = 0; x< contacts.length; x ++){
var user = contacts [x];
//console.log (user);
//通过id获取旧联系人
//设置用于查找conact的选项
var options = new ContactFindOptions();
options.filter = user.id; //要搜索的名称
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 {
联系人.photos [0] = {
pref:false,
type:'url',
value:user.iceTV.pp
};
}
c.save(function(saveSuccess){
console.log('User updated',c);
},function(saveError){
console .log(更新时出错);
});
},function(e){
makeToast(Error =+ e.code);
},options);
}


解决方案

通过向电话号码添加 id 来修复它。对于您的示例代码,您必须更改的行应如下所示:

  c.phoneNumbers [0] = {type :'mobile',value:user.iceTV.number,id:0}; 

:这只修复了重复。当您删除数据库中的phoneNumber时,它不会在联系人中删除。有关详细信息,请参见我的问题。 p>

I have feature in my app that checks users contacts using cordova contacts plugin with an online database of users. If the any of the contacts in the users device have accounts the data from the account is updated on the device.

The function works with things like name and date of birth but emails and phone numbers don't overwrite the existing contact data it instead creates new entry for these.

This particular user should only have 1 email address and 1 phone number.

JSON reponse and save

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);
}

解决方案

I had the same issue and fixed it by adding an id to the phone numbers. For your example code the line you'd have to change would look like this:

c.phoneNumbers[0] = {type : 'mobile', value: user.iceTV.number, id: 0}; 

But: This only fixes the duplication. When you remove a phoneNumber in the database, it won't be removed in the contact. See my question here for further details.

这篇关于cordova联系人插件联系人数据正在复制,而不是覆盖联系人数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆