【发布时间】:2016-12-09 21:36:30
【问题描述】:
我的 index.js 文件中有以下代码。我可以打印配置文件表的数据。而且我还需要在同一(index.njk)页面中打印简历表的数据。但我做不到。我还找到了similar question,但我是新手,无法根据我的项目修改这些代码。你能帮忙吗?
var express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
router = express.Router(),
app = express();
var pg =require('pg');
// DB Connect string
var connect = {
user: 'arslan',
database: 'resumedb',
password: '1984',
host: 'localhost',
port: 5432,
max: 10,
idleTimeoutMillis: 30000,
};
router.get('/', function(req, res){
pg.connect(connect, function(err, client, done, skills){
if(err){
return console.error('errrr', err)
}
//Get Profile Informations
client.query('select id,fname,lname,title,description,profileimage from profile', function(err, result){
if(err){
return console.error('error running query', err);
}
if(result.rows.length > 0) {
res.render('index.njk', {
profileName: result.rows[0].fname,
profileLName: result.rows[0].lname , profileTitle: result.rows[0].title
, profileDesc: result.rows[0].description
, profileImage: result.rows[0].profileimage
});
console.log(result.rows[0].profileimage);
}else {
console.log('No rows found in DB');
}
done()
});
});
});
【问题讨论】:
-
查看pg-promise,这是构建依赖和独立查询的最简单方法;)
标签: node.js postgresql express nunjucks