Express.js - Retrieve one row from MySQL database

I'm trying to retrieve a single row from the mysql database and display it on the page (using handlebars).

This is my API:

//app.js
app.get('/human/view/:request', api.getHuman);

---------------

//api.js
var mysql = require('mysql');

var client = mysql.createConnection({
host        :   'localhost',
user        :   'vlad',
password    :   'xxx',
database    :   'resources'
});

exports.getHuman = function(req, res) {

client.query('SELECT * FROM humans WHERE request = ?', req.params.request, function(err, result) {
    if (err) {
        throw err;
    }

    res.render('viewHuman', { title: 'View Resources', result: result });
});
}

-----------------------------------------

// viewHuman View

<h2>{{ result.firstname }} + ' ' + {{ result.lastName }}</h2>
<hr>

<h4>Request ID</h4>
<p> {{ result.request }} </p>
<hr>
.......

Still, whenever I try to retrieve that entry, I get the view rendered, but all my fields are empty.

I am looking forward to your feedback!

Thank you!

Answers


Change this line

    res.render('viewHuman', { title: 'View Resources', result: result[0] });

The callback return rows, as an array of objects, so just pass the first one to the view.


Need Your Help

NASM versus AT&T Syntax: what are the advantages?

assembly nasm att

I'm going through the Intel processor documentation and writing some basic assembly code at the same time. I have both nasm and as (GAS) on my server and I understand the basic differences of both

Android USB-Debugging: Device recognized but then loses connection

android debugging connection usb

I followed the steps for USB Debugging showed here under paragraph 8 and my HTC Desire is recognized once:./adb devices

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.