enesser / vCards-js

Create vCards to import contacts into Outlook, iOS, Mac OS, and Android devices from your website or application.
MIT License
419 stars 154 forks source link

How can i create one Vcard using many Json objects? #17

Closed AP25 closed 8 years ago

AP25 commented 8 years ago

I can create one Vcard(vcf file) from one object. But I want to create one Vcard(vcf file) from multiple objects. e.g : I have 3 Json objects such as [ { N: 'Stenerson,Derik', FN: 'Derik Stenerson', ORG: 'Microsoft Corporation', TITLE: '', PHOTO: { TYPE: '', PHOTO: '' }, TEL: [ [Object], [Object] ], ADR: [], EMAIL: 'deriks@Microsoft.com' }, { N: 'Ganguly,Anik', FN: 'Anik Ganguly', ORG: ' Open Text Inc.', TITLE: '', PHOTO: { TYPE: '', PHOTO: '' }, TEL: [ [Object] ], ADR: [], EMAIL: 'ganguly@acm.org' }, { N: 'Moskowitz,Robert', FN: 'Robert Moskowitz', ORG: '', TITLE: '', PHOTO: { TYPE: '', PHOTO: '' }, TEL: [], ADR: [], EMAIL: 'rgm-ietf@htt-consult.com' } ]

and want to create one Vcard such as Xyz.vcf then how to create it ?

Thanks in advance

enesser commented 8 years ago

Thanks for asking.

I'm leaving this issue open because I am probably going to make an enhancement to the framework that will let you do this in a very easy way. However, until then, I will show you how you can do it with a workaround.

Per your example:


'use strict';

var contacts = [{
    N: 'Stenerson,Derik',
    FN: 'Derik Stenerson',
    ORG: 'Microsoft Corporation',
    EMAIL: 'deriks@Microsoft.com'
}, {
    N: 'Ganguly,Anik',
    FN: 'Anik Ganguly',
    ORG: ' Open Text Inc.',
    EMAIL: 'ganguly@acm.org'
}, {
    N: 'Moskowitz,Robert',
    FN: 'Robert Moskowitz',
    ORG: '',
    EMAIL: 'rgm-ietf@htt-consult.com'
}];

var vCard = require('vcards-js');
var vContact;
var formattedString = '';

contacts.forEach(function(contact) {
    vContact = vCard();
    vContact.firstName = contact.N.split(',')[1];
    vContact.lastName = contact.N.split(',')[0];
    vContact.organization = contact.ORG;
    vContact.email = contact.EMAIL;
    formattedString += vContact.getFormattedString();
});

var fs = require('fs');
fs.writeFileSync('xyz.vcf', formattedString, { encoding: 'utf8' });
AP25 commented 8 years ago

Thanks Enesser !!!!