0
votes

I am using node + express for backend , postgresql for database, EJS for front-end.

I am using pm2 package for server start/stop/logs.

Some times the API query error means the server not respond after 1/2 minutes getting 504 gateway timeout error.

Example : I am accessing http://mysiteurl/index url in this page I had some error or exception means, I tried to connect another page http://mysiteurl/about , this page also getting the error : 504 gateway timeout

some times getting error in /index call

Error details :

{ [error: invalid input syntax for integer: "undefined"] 
 name: 'error', 
 severity: 'ERROR', 
 code: '22P02', 
 detail: undefined, 
 hint: undefined, 
 position: '87', 
 internalPosition: undefined, 
 internalQuery: undefined, 
 where: undefined, 
 file: 'numutils.c', 
 line: '62', 
 routine: 'pg_atoi' } 

For example here my code for /index and /about page code:

router.get('/index', function(req, res) {

    globaldetails.video(userId, function(err, videoList) {
        users.getUserDetails(userId, function(err, userDetails) {
            globaldetails.expertContent(userId, function(err, expertContent) {
                res.render('users/index', {
                    video : videoList.rows,
                    userDetails : userDetails.rows,
                    expertContent : expertContent.rows,
                });
            });
        });

    });

});

router.get('/about', function(req, res) {
    res.render('users/about');

});

function video(userId, callback) {
    client.query("select * from video where userId='" + userId + "' ", function(err, video) {
        console.log(err);
        callback(err, video);
    });

}

function getUserDetails(userId, callback) {
    var query = "select * from users where userId='" + userId + "' ";
    console.log(query);
    client.query(query, function(err, result) {
        if (err) {
            console.log(err);
        } else {
            callback(err, result);
        }

    });
}

function expertContent(userId, callback) {
    var query = "select * from content where userId='" + userId + "' ";
    client.query(query, function(err, expertContent) {
        if (err) {
            console.log(err);
        } else {
            callback(err, result);
        }
    });

}

Anyone help for this.

Expect result : when I got the error /index api call it should not affect the /page api call.

1

1 Answers

0
votes

Problem here maybe userId is undefined, log it to check. You didn't have error handler in your app , it will make your app freeze

router.get('/index', function(req, res,next) {
  globaldetails.video(userId, function(err, videoList) {
    if(err) next(err); // do same things will other callback or change your code to Promise to easy debug
    else {
    users.getUserDetails(userId, function(err, userDetails) {
        globaldetails.expertContent(userId, function(err, expertContent) {
            res.render('users/index', {
                video : videoList.rows,
                userDetails : userDetails.rows,
                expertContent : expertContent.rows,
            });
        });
    });}
});
});

You also need logic handle all error

app.use(function (err, req, res, next) {
    res.render(error page with err info)
})