【发布时间】:2016-12-01 22:31:58
【问题描述】:
我正在开发一个 NodeJs 应用程序,该应用程序从 FB 获取事件并将其放入本地数据库。对于 api 查询第一页中的每个事件,除了最后一个之外,这一切都很好。
我收到以下错误:
[2016 年 12 月 1 日,下午 1:48:39] 类型错误:无法读取属性“名称” 未定义的 在传入消息。 (/home/node/virgo/app.js:217:32) 在 IncomingMessage.emit (events.js:129:20) 在 _stream_readable.js:907:16 在 process._tickCallback (node.js:372:11)
紧接着
console.log(resultBody);
代码:
function addFBEvent(facebookId){
console.log("getting event: " + facebookId);
var options = {
hostname: 'graph.facebook.com',
port: 443,
path: '/v2.8/'+facebookId+'?fields=name,description,start_time,end_time,place,photos{images}&access_token={INSERT API ACCESS CODE HERE}',
method: 'GET'
};
https.request(options, function(res2) {
var resultBody = "";
res2.setEncoding('utf8');
res2.on('data', function (chunk) {
resultBody = resultBody + chunk;
});
res2.on('end', function () {
dbConnection = sql.createConnection({
host : settings.dbHost,
user : settings.dbUser,
password : settings.dbPassword
});
dbConnection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
var json = JSON.parse(resultBody);
console.log(resultBody);
if (json != undefined){
var eventName = json.name;
var eventStart = json.start_time;
var eventEnd = json.end_time;
var eventDescription = json.description;
var eventPlace = json.place.name;
var eventPoster = json.photos.data[json.photos.data.length-1].images[0].source;
var eventId = json.id;
console.log("name: " + eventName + ", start: " + eventStart + ", end: " + eventEnd + ", place: " + eventPlace + ", Poster: " + eventPoster);
//console.log("Description: " + eventDescription);
dbConnection.query('INSERT INTO SVVirgo.activities(title, description, image, start, end, price, location, facebook) VALUES ("'+eventName+'","'+eventDescription+'","'+eventPoster+'","'+eventStart+'","'+eventEnd+'",0,"'+eventPlace+'","'+eventId+'")', function (err, result){
});
}
dbConnection.end();
})
}).end();
}
查看图表 api explorer 了解中断事件:Graph API
事件代码:1682486658666506
我尝试捕获未定义的 json 对象但没有成功,我还尝试使用此解决方案验证 json:Stackoverflow validate JSON javascript
【问题讨论】:
-
我用更多信息更新了我的答案。
-
它正在正确检查未定义,但您在此处遇到错误
var eventPlace = json.place.name;
标签: javascript json node.js facebook-graph-api https