0

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 Answer 1

0

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)
})
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't understand. please can you explain little briefly about router.get('/index', function(req, res,next) { logic handle if err , next(err) });
Its working but have anyother methods, because I have many functions for example. router.get('/index', function(req, res, next) { test1(userId, function(err, videoList) { test2(userId, function(err, userDetails) { test3(userId, function(err, expertContent) { test4(userId, function(err, expertContent) { test5(userId, function(err, expertContent) { test6(userId, function(err, expertContent) { res.render('users/index', { video : videoList.rows, userDetails : userDetails.rows, expertContent : expertContent.rows, }); }); }); }); }); }); }); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.