我需要你的帮助,因为我在Postgresql中存储数据时一直在努力。我预计postgresql和app的实例正在Bluemix平台上运行。无论如何,这个问题与Postgres有关。
在这里,您可以找到我目前用于在Postgresl中存储数据的代码:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var Q = require ('q');
var cfenv = require('cfenv');
const util = require('util');
const assert = require('assert');
var pg = require('pg');
var appEnv = cfenv.getAppEnv();
var services = appEnv.services;
var pg_services = services["compose-for-postgresql"];
// assert(!util.isUndefined(pg_services), "Must be bound to compose-for-postgresql services");
var credentials = pg_services[0].credentials;
var ca = new Buffer(credentials.ca_certificate_base64, 'base64');
var connectionString = credentials.uri;
var parse = require('pg-connection-string').parse;
config = parse(connectionString);
config.ssl = {
rejectUnauthorized: false,
ca: ca
}
var client = new pg.Client(config);
// This function to set up the connection with PostgreSQL database
module.exports.postgresql_database_connection = function() {
console.log("creating table");
client.connect(function(err) {
if (err) {
console.log("error connecting");
console.log(err);
}
else {
client.query('CREATE TABLE IF NOT EXISTS users (email varchar(256) PRIMARY KEY NOT NULL, name varchar(256) NOT NULL, surname varchar(256) NOT NULL, telephone varchar(256) NOT NULL, role varchar(256) NOT NULL, description varchar(256))', function (err,result){
if (err) {
console.log("error with query");
console.log(err);
}
else {
console.log("table created");
}
});
}
});
};
// This function is to create and store a new user into the PostgreSQL database with all the needed information
module.exports.postgresql_save_user = function(email, name, surname, role, telephone, description) {
console.log("reading parameters");
console.log("reading email : " + email);
var deferred = Q.defer();
// set up a new client using our config details
var client = new pg.Client(config);
client.connect(function(err) {
if (err) {
console.log("errore 1");
console.log(err);
deferred.reject();
}
else {
var queryText = 'INSERT INTO users(email,name,surname,telephone,role,description) VALUES(?, ?, ?, ?, ?, ?)';
// client.query(queryText, [email, name, surname, telephone, role, description], function (error,result){
client.query(queryText, ['email', 'name', 'surname', 'telephone', 'role', 'description'], function (error,result){
if (error) {
console.log("errore 2");
console.log(error);
deferred.reject();
}
else {
console.log("Saving the new user into the postegresql database: ");
console.log(result);
//check how result is printed and then manage it where called
deferred.resolve(result);
}
});
}
});
return deferred.promise;
};
我遇到以下错误,我不明白它是什么 - 我已经检查过它并且表已成功创建。问题应该是
2017-06-19T14:09:51.54+0100 [APP/0] OUT { error: syntax error at or near ","
2017-06-19T14:09:51.54+0100 [APP/0] OUT at Connection.parseE (/home/vcap/app/node_modules/pg/lib/connection.js:567:11)
2017-06-19T14:09:51.54+0100 [APP/0] OUT at Connection.parseMessage (/home/vcap/app/node_modules/pg/lib/connection.js:391:17)
2017-06-19T14:09:51.54+0100 [APP/0] OUT at TLSSocket.<anonymous> (/home/vcap/app/node_modules/pg/lib/connection.js:129:22)
2017-06-19T14:09:51.54+0100 [APP/0] OUT at emitOne (events.js:96:13)
2017-06-19T14:09:51.54+0100 [APP/0] OUT at TLSSocket.emit (events.js:188:7)
2017-06-19T14:09:51.54+0100 [APP/0] OUT at readableAddChunk (_stream_readable.js:176:18)
2017-06-19T14:09:51.54+0100 [APP/0] OUT at TLSSocket.Readable.push (_stream_readable.js:134:10)
2017-06-19T14:09:51.54+0100 [APP/0] OUT at TLSWrap.onread (net.js:551:20)
2017-06-19T14:09:51.54+0100 [APP/0] OUT name: 'error',
2017-06-19T14:09:51.54+0100 [APP/0] OUT length: 83,
2017-06-19T14:09:51.54+0100 [APP/0] OUT severity: 'ERROR',
2017-06-19T14:09:51.54+0100 [APP/0] OUT code: '42601',
2017-06-19T14:09:51.54+0100 [APP/0] OUT hint: undefined,
2017-06-19T14:09:51.54+0100 [APP/0] OUT position: '81',
2017-06-19T14:09:51.54+0100 [APP/0] OUT internalPosition: undefined,
2017-06-19T14:09:51.54+0100 [APP/0] OUT internalQuery: undefined,
2017-06-19T14:09:51.54+0100 [APP/0] OUT where: undefined,
2017-06-19T14:09:51.54+0100 [APP/0] OUT column: undefined,
2017-06-19T14:09:51.54+0100 [APP/0] OUT dataType: undefined,
2017-06-19T14:09:51.54+0100 [APP/0] OUT file: 'scan.l',
2017-06-19T14:09:51.54+0100 [APP/0] OUT line: '1081',
2017-06-19T14:09:51.54+0100 [APP/0] OUT routine: 'scanner_yyerror' }
答案 0 :(得分:1)
您使用的PostgreSQL驱动程序不支持语法VALUES(?, ?, ?, ?, ?, ?)'
。
它仅支持VALUES($1, $2, $3, $4, $5, $6)
格式化变量值。