gpt4 book ai didi

javascript - 使用 node.js 读写 vcard

转载 作者:行者123 更新时间:2023-11-30 12:40:01 25 4
gpt4 key购买 nike

我正在尝试为 vcard 创建和读取两者,在 googleng 了一段时间后没有找到合适的例子,是否有任何 vcard 创建者/阅读器可以开始,

我正在尝试这个 https://www.npmjs.org/package/vcard

我的代码

app.get('/',function(req,res){
//https://github.com/Fusselwurm/vcard-js/commit/dafd17ffd226c6170849be82e9c5dae94e23b8f0
card.readFile("./public/sample.vcf", function(err, json1) {
if(err){
console.log("vcard err "+err);
res.json(util.inspect(json1));
}
else
{
console.log("vcard json "+json1);
console.log("vcard json "+util.inspect(json1));
res.json(util.inspect(json1));
}


});
});

示例.vcf文件

BEGIN:VCARD VERSION:3.0 N:Gump;Forrest;;Mr. FN:Forrest Gump ORG:Bubba Gump Shrimp Co. TITLE:Shrimp Man TEL;TYPE=HOME,VOICE:(111) 555-1212 PHOTO;VALUE=URL;TYPE=GIF:http://www.example.com/dir_photos/my_photo.gif TEL;TYPE=WORK,VOICE:(111) 555-12121 ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com REV:2008-04-24T19:52:43Z END:VCARD BEGIN:VCARD VERSION:3.0 N:Gump;Forrest;;Mr. FN:Forrest Gump ORG:Bubba Gump Shrimp Co. TITLE:Shrimp Man TEL;TYPE=HOME,VOICE:(111) 555-1212 PHOTO;VALUE=URL;TYPE=GIF:http://www.example.com/dir_photos/my_photo.gif TEL;TYPE=WORK,VOICE:(111) 555-12121 ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com REV:2008-04-24T19:52:43Z END:VCARD

日志输出:它仅适用于 sample.vcf 中的 1 个 vcard 数据,但是当它包含更多数据时,它就会出错IEvCard 数据无效:缺少一个或多个必需元素(VERSION 和 FN)

最佳答案

我认为这里有几个问题:

  1. 您正在使用的 vcard 库尚未完成
  2. 您使用的 vcard 格式不正确

改为查看 vcardparser 库。

var vcardparser = require('vcardparser');

vcardparser.parseFile("./sample.vcf", function(err, json) {
if(err)
return console.log(err);
console.log(json);
});

这是一个示例电子名片:

BEGIN:VCARD
VERSION:4.0
N:Gump;Forrest;;;
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;MEDIATYPE=image/gif:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=work,voice;VALUE=uri:tel:+1-111-555-1212
TEL;TYPE=home,voice;VALUE=uri:tel:+1-404-555-1212
ADR;TYPE=work;LABEL="100 Waters Edge\nBaytown, LA 30314\nUnited States of America"
:;;100 Waters Edge;Baytown;LA;30314;United States of America
ADR;TYPE=home;LABEL="42 Plantation St.\nBaytown, LA 30314\nUnited States of America"
:;;42 Plantation St.;Baytown;LA;30314;United States of America
EMAIL:forrestgump@example.com
REV:20080424T195243Z
END:VCARD

vcardparser 的输出

{ begin: 'VCARD',
version: '4.0',
n:
{ last: 'Gump',
first: 'Forrest',
middle: '',
prefix: '',
suffix: '' },
fn: 'Forrest Gump',
org: { name: 'Bubba Gump Shrimp Co.', dept: undefined },
title: 'Shrimp Man',
photo:
{ type: [],
value: 'http://www.example.com/dir_photos/my_photo.gif' },
tel:
[ { type: [Object], value: 'tel:+1-111-555-1212' },
{ type: [Object],
value: 'tel:+1-404-555-1212ADR;TYPE=work;LABEL="100 Waters Edge\\nBaytown, LA 30314\\nUnited States of America"' } ],
' ': ';;100 Waters Edge;Baytown;LA;30314;United States of AmericaADR;TYPE=home;LABEL="42 Plantation St.\\nBaytown, LA 30314\\nUnited States of America"',
' ': ';;42 Plantation St.;Baytown;LA;30314;United States of America',
email: [ { type: [], value: 'forrestgump@example.com' } ],
rev: '20080424T195243Z',
end: 'VCARD' }

关于javascript - 使用 node.js 读写 vcard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24817820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com