Extracting results from SELECT * in node pg

I have a Postgresql stored function defined as follows:

CREATE OR REPLACE FUNCTION SessionGet(
    sid            varchar)
    RETURNS "SESSION" AS $$
    SELECT * FROM "SESSION" WHERE "SESSION_ID" = sid;
$$ LANGUAGE sql;

I call it from node using the PG module with:

SELECT SessionGet('SID-1'::varchar);

The table is sessiondefined as:

CREATE TABLE "SESSION"
(
  "SESSION_ID" character varying NOT NULL DEFAULT ''::character varying,
  "SESSION" json NOT NULL DEFAULT '{}'::json,
  "LAST_UPDATE" bigint DEFAULT 0,
  CONSTRAINT "SESSION_PK" PRIMARY KEY ("SESSION_ID")
)
WITH (
  OIDS=FALSE
);

I am trying to get the return result as follows:

client.query(sql, function(err, result) {
            done(); // Releasing connection to the pool
            if ( err ) {
                callback(err);
            } else if ( result.rows.length > 0 ) {
                var ttmp = result.rows[0];
                var tmp1 = ttmp[0];
                console.log("Res[0]: " + tmp1);
                callback(err,result.rows[0]);
            } else {
                callback(err);
            }
        });

Although result.rows.length- > 0, result.rows[0][0]- is undefined. How to get field values ​​from returned string?

+3
source share
1 answer

I solved the problem by sending an SQL ( SELECT * FROM "SESSION" WHERE "SESSION_ID" = '...';) statement instead of relying on a stored function.

I also changed the column name SESSIONto SESSION_JS, as pointing the table name to the column also seems to be a problem, although the error message is not displayed.

0

All Articles