Search code examples
node.jsexpressexpress-handlebars

Rendering data in handlebars using express framework


Hello I am working in Express Framework, I am using handlebars to render the data from my mysql table. While trying to render the data using below code,instead of rendering value it displaying [object object]. I posted my code below.

index.js:

 var query = connection.query('SELECT * FROM requestor_auth WHERE question_id = ? AND answer = ? AND app_key = ?  LIMIT 1', [data.qid, data.ansvalue, data.appid], function(err,rows)
    {

        if(err) {
            console.log("Error Selecting : %s ",err );
            res.redirect('/');
        } else {
            res.render('requestform',{page_title:"Edit Customers - Node.js",data:rows});
          }

requestform.hbs:

    <div class="addressto">
              <h4>To,</h4>
                <br>
                  <span style="font-size:18px;margin-left:10px;">The Collector Of</span>
                  <input type="text" value="{{data}}" class="line" class="text-line" style="margin-left:35px;"><br>

    </div>

The value in the form input displaying as [object object]. I tried as data.key_value to render the data but it is not displaying the value. Please give me a solution. Thank you.


Solution

  • Because the result of Mysql response is array so it should be:

     var query = connection.query('SELECT * FROM requestor_auth WHERE question_id = ? AND answer = ? AND app_key = ?  LIMIT 1', [data.qid, data.ansvalue, data.appid], function(err,rows) {
    
            if(err) {
                console.log("Error Selecting : %s ",err );
                res.redirect('/');
            } else {
                res.render('requestform',{page_title:"Edit Customers - Node.js",data:rows[0]});
              }
    

    If there's a same error you should console.log() your result to check the value.