var findOne = function(username, cb) {
pg.connect(pgURL, function(err, client, done) {
var query = client.query('SELECT * FROM users WHERE username = $1', [username]);
done();
query.on('row', function(row) {
cb(err, row);
});
});
};
但是,如果我在没有参数化的情况下执行SQL语句,它就可以正常工作。
var query = client.query("SELECT * FROM users WHERE username = 'foobar'");
此外,在done()之后执行console.log(查询)会产生以下结果:
13:48:00 web.1 | { domain: null,
13:48:00 web.1 | _events: null,
13:48:00 web.1 | _maxListeners: 10,
13:48:00 web.1 | name: undefined,
13:48:00 web.1 | text: 'SELECT * FROM users WHERE username = $1',
13:48:00 web.1 | values: [ '{"username":"foobar"}' ],
13:48:00 web.1 | callback: undefined,
13:48:00 web.1 | _result:
13:48:00 web.1 | { command: null,
13:48:00 web.1 | rowCount: null,
13:48:00 web.1 | oid: null,
13:48:00 web.1 | rows: [],
13:48:00 web.1 | fields: [],
13:48:00 web.1 | _parsers: [],
13:48:00 web.1 | RowCtor: null },
13:48:00 web.1 | _addedFields: false,
13:48:00 web.1 | _canceledDueToError: false }
我在这里做错了什么?
答案 0 :(得分:1)
您的用户名变量似乎是一个具有密钥用户名和值foobar的Javascript对象。请尝试username.username
仅引用该值。
或者,我强烈推荐ORM Sequelize,它位于pg之上。看看这里:http://sequelizejs.com/documentation#usage-raw-queries
答案 1 :(得分:0)
您确定用户名是字符串吗?尝试username.toString()
。