我是postgres的新手,并尝试做一个简单的选择查询。
我的表格如下:
create table push_table
(
seq_id int,
device_id text,
primary key (seq_id),
user_id varchar(100)
);
和我的nodejs代码如下:
pool.connect(function (err, client, done)
{
if (err) console.log(err)
myClient = client
//query if the user exists
var ageQuery = format('SELECT * from push_table WHERE user_id = ' + userId);
myClient.query(ageQuery, function (err, result) {
if (err) {
console.log(err)
}
console.log("number of rows:" + result.rows.length);
});
});
运行nodejs
服务器日志打印:
{ [error: missing FROM-clause entry for table "catherine"]
name: 'error',
length: 113,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '42',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '3129',
routine: 'errorMissingRTE' }
/data/OSC_Push_OOW/releases/20170703215021/api/controllers/pushcontroller.js:58
console.log("number of rows:" + result.rows.length);
^
TypeError: Cannot read property 'rows' of undefined
at null.callback (/data/OSC_Push_OOW/releases/20170703215021/api/controllers/pushcontroller.js:58:46)
at Query.handleError (/data/OSC_Push_OOW/shared/node_modules/pg/lib/query.js:161:17)
at null.<anonymous> (/data/OSC_Push_OOW/shared/node_modules/pg/lib/client.js:188:26)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at Socket.<anonymous> (/data/OSC_Push_OOW/shared/node_modules/pg/lib/connection.js:133:12)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
答案 0 :(得分:1)
列user_id是varchar:
user_id varchar(100)
因此有效的SQL查询必须使用单引号来定义varchar值,例如:
SELECT * from push_table WHERE user_id = 'Vik'
您的代码不提供所需的报价。