0

this is my js file code:

fs.readFile(__dirname+'/product_list.html',function(error,productForm){
    if(error){
        response.write("error in getting file"+error);
    }else{
            connection.query("select * from products",function(err,result){
            if(err){
                console.log("In funciton errr");
            }else{
                response.writeHead(200, "OK", {'Content-Type': 'text/html'});
                response.write(productForm,{result:result});

                }
        });
    response.end();
    }
});

i want result on product_list.html page.....

2
  • you can use a template engine as ejs or other one it will be faster in this way and cleaner Commented Mar 31, 2016 at 13:24
  • i render on product_list.html page but...not getting result object on that page Commented Mar 31, 2016 at 13:26

1 Answer 1

1

The problem you have is that connection.query is asynchronous, you should call response.end() within the callback function of connection.query

fs.readFile(__dirname+'/product_list.html',function(error,productForm){
    if(error){
        response.write("error in getting file"+error);
        response.end(); //finish here
    }else{
        connection.query("select * from products",function(err,result){
            if(err){
                console.log("In funciton errr");
                response.end(); //finish here
            }else{
                response.writeHead(200, "OK", {'Content-Type': 'text/html'});
                response.write(productForm,{result:result});
                response.end(); //finish here
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

thank for answer.....after add this code...same problem is their..... render page but not getting result object in product_list.html page
what is productForm and {result: result}? are you using any template engine? then it should be response.render(productForm, {result: result})

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.