本文介绍了节点JS忽略未定义的检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个NodeJs应用程序上工作,该应用程序从FB获取事件并将其放入本地数据库。对于api查询的第一页中的每个事件,除了最后一个事情之外,这一点很好。

Im working on a NodeJs app that takes an event from FB and puts it into a local database. For every event in the first page of the api query this goes well, except for the last one.

我收到以下错误:

刚刚

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资源管理器的突破事件:

See graph api explorer for the breaking event: Graph API

事件代码:1682486658666506

Event code: 1682486658666506

我试过抓住未定义的json对象没有运气,我也尝试使用这个解决方案验证json:

I've tried catching the undefined json object with no luck, i've also tried to validate the json using this solution: Stackoverflow validate JSON javascript

推荐答案

而不是 https.request 尝试使用。

Instead of https.request try using request.

它会给你解析JSON,你不必这样做手动

It will give you parsed JSON and you won't have to do it manually.

如果你想像你这样做手动,那么记住要包装 var json = JSON.parse(resultBody); 在try / catch块中(或使用),因为 JSON.parse 不能在try / catch之外的未知数据上使用 - 它可以抛出异常。

If you want to do it manually like you do, then remember to wrap var json = JSON.parse(resultBody); in a try/catch block (or use tryjson) because the JSON.parse cannot be used on unknown data outside of a try/catch - it can throw exceptions.

另外一点,不要打开你的路由处理程序中的数据库连接。你应该打开一次连接,只需在你的处理程序中使用它。有关详细信息,请参阅。

Another thing, don't open you database connection in your route handlers. You should open the connection once and just use it in your handlers. See this answer for more info about it.

现在,您正在连接到数据库,但您继续在连接回调之外,所以您运行所有从 var json = JSON.parse(resultBody)开始的行; 之前建立数据库连接。

Right now you are connecting to the database but you continue outside of the connection callback, so you run all the lines beginning from var json = JSON.parse(resultBody); before the DB connection is established.

此外,错误可能不是因为 json 未定义,但因为 json.place 未定义。

Additionally, the error may be not because json is undefined but because json.place is undefined.

您可以更改这个:

var eventPlace = json.place.name;

到:

var eventPlace = json.place && json.place.name;

您还必须先检查 json.photos 您访问 json.photos.data 并测试如果 json.photos.data 是一个数组,然后再将其视为(你可以这样做:

You must also check json.photos before you access json.photos.data and test if json.photos.data is an array before you treat it as such (you can do it with:

if (json.photos && Array.isArray(json.photos.data)) {
  // ...
}

基本上,你需要确保这些值是您希望在访问它们之前的值。例如,访问以下内容:

Basically, you need to make sure the values are what you want them to be before you access them. For example accessing this:

json.photos.data[json.photos.data.length-1].images[0].source

可以失败 json 未定义,当 json.photos 未定义时, json.photos.data 未定义,当 json.photos.data 不是数组时,当 json.photos.data.length 为零时,当 json.photos.data [lson.photos.data.length-1] 未定义时,当 json.photos 。数据[json.photos.data.length-1] .images 未定义,当 json.photos.data [json.photos.data.length-1] .images 不是数组,当 json.photos.data [json.photos.data.length-1] .images 是一个数组,但它是空的,或者当 json.photos.data [ json.photos.data.length-1] .images 是一个非空数组,但 json.photos.data [json.photos.data.length-1] .images [0] .source 未定义。

can fail when json is undefined, when json.photos is undefined, when json.photos.data is undefined, when json.photos.data is not an array, when json.photos.data.length is zero, when json.photos.data[json.photos.data.length-1] is undefined, when json.photos.data[json.photos.data.length-1].images is undefined, when json.photos.data[json.photos.data.length-1].images is not an array, when json.photos.data[json.photos.data.length-1].images is an array but it's empty, or when json.photos.data[json.photos.data.length-1].images is a non-empty array but json.photos.data[json.photos.data.length-1].images[0].source is undefined.

正如你所看到的,你在这里做了很多假设。当这些假设不符合时,代码将失败。

As you can see there are a lot of assumptions that you are doing here. When those assumptions are not met, the code will fail.

这篇关于节点JS忽略未定义的检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:59