Postgresql数据库查询返回空数组

时间:2018-07-06 16:26:28

标签: javascript node.js database postgresql

我正在尝试使用pg node从node.js查询一个postgresql数据库。问题是它不断返回一个空数组而没有错误消息。我使用安装在计算机上的PGAdmin作为数据库软件。请我帮忙。这是下面的代码

import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import consolidate from 'consolidate';
import dust from 'dustjs-helpers';
import {Client} from 'pg';


let app = express();

//Create DB connection string
//postgres://username:password@localhost/database;
// let conn = "postgresql://knowshare:gemshare@localhost:46713/recipebookdb";
const client = new Client({
	user: 'Knowshare',
	host: 'localhost',
	database: 'recipebookdb',
	password: 'gemshare',
	port: 3005
});
client.connect();

//assign Dust Engine to .dust files
app.engine('dust', consolidate.dust);

//Set default extension to .dust
app.set('view engine', 'dust');
app.set('views', __dirname + '/views');

//set public filder
app.use(express.static(path.join(__dirname, 'public')));

//body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));


app.get('/', (req, res)=>{
	console.log('i got here');
	client.query('SELECT * from recipes', (dbErr, dbRes)=>{
		if(dbErr){
			console.log(dbErr);
		}else{
			res.render('index', {recipes: dbRes.rows});
			console.log("********************\nRows:", dbRes.rows, "\n********************");
		}
		client.end();
	});
	// res.render('index');
});

app.listen(3000, ()=>{
	console.log('Server started on port 3000');
});

0 个答案:

没有答案