Kannan K
Kannan K

Reputation: 4461

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.

Upvotes: 0

Views: 2052

Answers (2)

Burdy
Burdy

Reputation: 611

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.

Upvotes: 1

Dirk De visser
Dirk De visser

Reputation: 1

The rows argument in your callback function is by a select query always an array of objects. With handlebars you should be able to do the following:

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

Also multiple class names can be in one class attribute.

Upvotes: 0

Related Questions