我正在学习Express和Postgres的Node JS,并且在制作CRUD应用程序时,当我执行INSERT INTO语句时遇到了这样的错误。
--set
我的代码如下:
(node:4998) UnhandledPromiseRejectionWarning: TypeError: Invalid Query Result Mask specified.
at Database.$query (/home/aabishkar/Documents/meanstack/node_modules/pg-promise/lib/query.js:121:25)
at Database.<anonymous> (/home/aabishkar/Documents/meanstack/node_modules/pg-promise/lib/query.js:259:23)
at config.$npm.connect.pool.then.db (/home/aabishkar/Documents/meanstack/node_modules/pg-promise/lib/database.js:326:42)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7) (node:4998) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3) (node:4998) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
数据库已连接。还可以参考以下代码。当我尝试动态插入值时出现错误。
exports.dynamicadd = function(req, res) {
var name = req.body.name;
var dob = req.body.dob;
var gender = req.body.gender;
var values = [name, gender, dob];
console.log(values); //Up to here code is fine. It returns the value.
//from here the error comes
var sql = "INSERT INTO students (name, gender, dob) VALUES ?";
db.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
});
res.redirect('/');
}
谢谢。
答案 0 :(得分:1)
pg-promise返回一个promise
,不需要回调。您使用的库不正确。当值是第二个参数时,则第三个参数不应被视为回调,而应被称为QueryResultMask
您应该使用的是:
db.query(...)
.then(r => console.log(r))
.catch(e => console.log(e))