我创建了一个名为 alpha 的表,并在 alpha 中创建了一个名为“event”的表。我可以向事件表添加一行,并从 Node 中的事件表中检索所有行。然而,当我在命令行中尝试时,当我选择全部时,我得到一个“空白”。
命令行(看不到任何行):
(master U:1 ✗) happy psql alpha
psql (9.4.4)
Type "help" for help.
alpha=# psql \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+------------
public | event | table | pgOwner
(1 row)
alpha-# select * from event
alpha-#
Node 代码(显示行):
let promise = require('bluebird');
let moment = require('moment');
let options = {
// Initialization Options
promiseLib: promise
};
let pgp = require('pg-promise')(options);
let connectionString = 'postgres://localhost:5432/alpha';
let db = pgp(connectionString);
// add query functions
function getAllEvents(req, res, next) {
db.any('select * from event')
.then(function (data) {
res.status(200)
.json({
status: 'success',
data: data,
message: 'Retrieved ALL events'
});
})
.catch(function (err) {
return next(err);
});
}
function addEvent(req, res, next) {
// let date = moment(new Date()).format('MMMM Do YYYY, h:mm A');
db.none('insert into event(firstName, lastName, eventDate, emotion, answer)' +
'values(${firstName}, ${lastName}, ${emotion}, ${emotion}, ${answer})',
req.body)
.then(function () {
res.status(200)
.json({
status: 'success',
message: 'Inserted one event'
});
})
.catch(function (err) {
return next(err);
});
}
module.exports = {
getAllEvents: getAllEvents,
addEvent: addEvent,
};
最佳答案
At the prompt, the user can type in SQL commands. Ordinarily, input lines are sent to the server when a command-terminating semicolon is reached. An end of line does not terminate a command.
换句话说,您必须以 ;
结束每条语句,这与通过驱动程序执行查询相反,当您执行多个语句时,驱动程序只需要 ;
使用单个请求进行查询(您通常不会这样做)。
关于node.js - 可以从 Node 获取数据,但不能使用 postgres 命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39749320/