javascript - Passing array of JSON to postgres without double backslash -


i working on node.js version v0.12.7
my goal input question_id array postgres database.
problem fail pass array of json postgres function
not solution in worst case try 1 one query.
my attempt convert input array of question_id example :
array [2,4,1,3]
json : [{ order : 1, question_id : 2}, { order : 2, question_id : 4}, { order : 3, question_id : 1},{ order : 3, question_id : 3} ]

then pass pg.

i convert array of integer array of json exports.intarray2json function.

exports.intarray2json = function(req){     return new promise(function(fulfill,reject){     var arr = [];     var in_array = req.body.questionlist;     for(var = 1 ; <= in_array.length ; i++){         var data = {order : i, question_id : in_array[i-1]};         arr.push(data);     }     fulfill(arr);     }); }; 

after that. stringify array of objects. , json.parse() it.
in short name them var , var b.
unfortunately, neither nor b works. when use in data array.

console.log of 'a' case:

pg receive :  [ 8,   '[{"order":1,"question_id":5},{"order":2,"question_id":2},{"order":3,"question_id":3},{"order":4,"question_id":1}]' ] 

error of 'a' case:

{ [error: cannot begin/end transactions in pl/pgsql]   name: 'error',   length: 235,   severity: 'error',   code: '0a000',   detail: undefined,   hint: 'use begin block exception clause instead.',   position: undefined,   internalposition: undefined,   internalquery: undefined,   where: 'pl/pgsql function dugong.quizquestion_jsadd(integer,json) line 20 @ sql statement',   schema: undefined,   table: undefined,   column: undefined,   datatype: undefined,   constraint: undefined,   file: 'pl_exec.c',   line: '3377',   routine: 'exec_stmt_execsql' } 

try 'b' case: console.log of 'b' case:

pg receive :  [ 8,   [ { order: 1, question_id: 5 },     { order: 2, question_id: 2 },     { order: 3, question_id: 3 },     { order: 4, question_id: 1 } ] ] 

error of 'b' case :

{ [error: invalid input syntax type json]   name: 'error',   length: 178,   severity: 'error',   code: '22p02',   detail: 'expected ":", found ",".',   hint: undefined,   position: undefined,   internalposition: undefined,   internalquery: undefined,   where: 'json data, line 1: {"{\\"order\\":1,\\"question_id\\":5}",...',   schema: undefined,   table: undefined,   column: undefined,   datatype: undefined,   constraint: undefined,   file: 'json.c',   line: '1136',   routine: 'report_parse_error' } 

here function in node.

exports.postquizquestion = function(client,req){     console.log('coming in function');     exports.test = function(array,callback){     console.log('hello in test function');     console.log(array);     for(var = 0 ; < array.length ; i++){         console.log(typeof(array[i]));         if(i == array.length -1){         callback(array);         }     }     };     exports.test2 = function(array,callback){     var = json.stringify(req.body.questionlist2);     var b = json.parse(a);      var data = [req.body.quizid, b];     console.log('pg receive : ');     console.log(data);     var qstr = "select dugong.quizquestion_jsadd($1,$2)";     var questlist = client.query(qstr, data, function(er,result){             if(er){         console.log(er);         var data = pgdown.rreject(er,req.params.quizid);         callback();         return;             }         console.log('quizquestion table updated');         callback();     });     };     exports.test(req.body.questionlist2, function(array){     exports.test2(array, function(){         console.log('end of function');     });     }); }; 

here table structure.

create table dugong.quizquestion(         quiz_id int,         order_question int,         question_id int,        --fk         lastmodified timestamp time zone default current_timestamp,         primary key(order_question,quiz_id),         foreign key(question_id) references dugong.question(question_id) on delete restrict     ,                constraint qzid_qid unique (quiz_id, question_id) ); 

my pg function :

create or replace function dugong.quizquestion_jsadd(        in_quiz_id int,        in_question_id_list json ) returns boolean $$ declare         idx integer;         json; begin         idx := 1;         in select * json_array_elements(in_question_id_list)         loop                 raise notice '%', idx;                 perform dugong.quizquestion_add(in_quiz_id, idx, i->>question_id);                 idx := idx + 1;         end loop;          return true;  exception when others         raise notice 'the transaction in uncommitable state.'                                                         'transaction rolled back.';         raise notice '% %', sqlerrm, sqlstate;         rollback; end; $$ language plpgsql; 


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -