0

我正在开发一个restful Api nodejs,我的第一步是使用数据结构,所有过程都很好,现在我想使用postgresql实现具有持久性的crud,我被困在post端点上,我的查询没有尽管我努力通过。

获取端点运行良好,所有必需品都已安装,只是在后端点上被阻止。在我的代码下面找到

router.post('/', function(req, res, next) {
pool.connect(function (err, client, done) {
if (err) {
    console.log("Can not connect to the DB" + err);
}
client.query('INSERT INTO users(firstname, lastname, othername, email, 
phoneNumber, username, registered, isAdmin) VALUES($1, $2, $3, $4, 5$, 
6$, 7$, $8) returning *',
[req.body.firstname, req.body.lastname, req.body.othername, 
req.body.email, req.body.phoneNumber, req.body.username, 
req.body.registered, req.body.isAdmin ], 
function (err, result) {
     done();
     if (err) {
         console.log(err);
         res.status(400).json({
           status : 400,
           error : err
         });
     }
     res.status(200).json({
       status : 200

      });
  });
});  

我没有在我的数据库中插入数据,而是收到以下错误

 {
"status": 400,
"error": {
    "name": "error",
    "length": 106,
    "severity": "ERREUR",
    "code": "42601",
    "position": "126",
    "file": "scan.l",
    "line": "1134",
    "routine": "scanner_yyerror"
}
}

在 Postman 上运行时,console.log(err) 显示了这个

{ error: erreur de syntaxe sur ou près de « $ »
at Connection.parseE (E:\Bootcamp_prep\MAPI\node_modules\pg\lib\connection.js:554:11)
at Connection.parseMessage (E:\Bootcamp_prep\MAPI\node_modules\pg\lib\connection.js:379:19)
at Socket.<anonymous> (E:\Bootcamp_prep\MAPI\node_modules\pg\lib\connection.js:119:22)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)  name: 'error',
4

2 回答 2

0

我不确定参数如何作为“$1, $2, $3”工作,但我 100% 确定您是否将它们替换为“?,?” sintaxe 它会起作用的。就像是:

const query = 'INSERT INTO users
 (firstname, lastname, othername, email, phoneNumber, username, registered, isAdmin) VALUES(?, ?, ?, ?, ?, ?, ?, ?)';

client.query(query,
[req.body.firstname, req.body.lastname, req.body.othername, req.body.email, req.body.phoneNumber, req.body.username, req.body.registered,req.body.isAdmin], 
function(){...})
于 2019-01-08T04:27:11.210 回答
0

问题已经解决......这是由于表列中的字母大写.. posgresql 不处理列中的大写.. thx any was

于 2019-01-09T17:43:00.167 回答