Matěj Zmítko
Matěj Zmítko

Reputation: 357

Nodejs localhost keeps loading my function

Good morning to everyone,

Please I would be so grateful if you could provide a little help for me. I am already stuck for long time with this issue.

I have a function which does not stop loading my localhost after this function is triggered. I did not figure out so far how to fix it.

Any help would be perfect.

This is my code:

// Copy scene
router.post('/copy', function(req,res,call) {

if( req.param('scene') !== undefined ){

db.serialize(function () {

 db.run("CREATE TABLE temp_table as SELECT * FROM scene where id=?", req.param('scene'));
 db.run("UPDATE temp_table SET id = NULL, user_id = (SELECT id FROM users WHERE email =?)",GLOBAL.email);
 db.run("INSERT INTO scene SELECT * FROM temp_table");
 db.run("DROP TABLE temp_table");
    if(error) {
        console.log(error);
    } 
 });

 db.close();

 }
 });

Thank you so much in advance

Upvotes: 1

Views: 514

Answers (2)

abdulbari
abdulbari

Reputation: 6232

Whenever browser sends any request to a server it expects a response. if it doesn't get any response it will be stuck with the timeout.

You need to send the response to terminate the request if you are not doing next execution with callback call or if you are expecting next manipulation then replace res.send() to call(error parameter,success parameter).

router.post('/copy', function(req, res, call) {

    if (req.param('scene') !== undefined) {

        db.serialize(function() {

            db.run("CREATE TABLE temp_table as SELECT * FROM scene where id=?", req.param('scene'));
            db.run("UPDATE temp_table SET id = NULL, user_id = (SELECT id FROM users WHERE email =?)", GLOBAL.email);
            db.run("INSERT INTO scene SELECT * FROM temp_table");
            db.run("DROP TABLE temp_table");
            if (error) {
                console.log(error);
                res.send(error);//send response if error
                //or call(error);
            }
            res.send({message:'success'});//send response if success
            //or call(null,whatever you want to pass)
        });
        db.close();

    }
});

Upvotes: 1

Vasan
Vasan

Reputation: 4956

You must either call the callback call() to pass on control to next middleware, or render and end the response using res.end() once your middleware logic is done.

Please see: https://expressjs.com/en/guide/writing-middleware.html

Upvotes: 0

Related Questions