简体   繁体   中英

Not able to store data in postgresql

I'd need your help since I'm struggling in storing data in Postgresql. I anticipate that the instance of postgresql and the app is running on Bluemix platform. Anyway the problem is related to Postgres.

Here you can find the code I'm currently using for storing data in 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;
};

I encounter the following error, and I don't understand what it is - I already checked it and the table was successfully created. The issue should be

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' }

PostgreSQL driver that you are using does not support syntax VALUES(?, ?, ?, ?, ?, ?)' .

It only supports VALUES($1, $2, $3, $4, $5, $6) to format variable values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM