我正在研究Node.js版本v0.12.7
我的目标是将Question_ID数组输入Postgres数据库
问题无法将json数组传递给Postgres函数
不好的解决方案在最坏的情况下,我会逐个查询
我的尝试是转换question_id的输入数组,例如:
从数组[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}] / p>
然后将其传递给pg。
我通过exports.IntArray2JSON函数成功将整数数组转换为JSON数组。
exports.IntArray2JSON = function(req){
return new Promise(function(fulfill,reject){
var arr = [];
var in_array = req.body.questionlist;
for(var i = 1 ; i <= in_array.length ; i++){
var data = {order : i, question_id : in_array[i-1]};
arr.push(data);
}
fulfill(arr);
});
};
之后。我将对象数组进行字符串化。和JSON.parse()它
简而言之,我将它们命名为var a和var b
不幸的是,a和b都不起作用。当我在数据数组中使用它时
Console.log&#39; a&#39;情况下:
pg receive :
[ 8,
'[{"order":1,"question_id":5},{"order":2,"question_id":2},{"order":3,"question_id":3},{"order":4,"question_id":1}]' ]
&#39; a&#39;错误案例:
{ [error: cannot begin/end transactions in PL/pgSQL]
name: 'error',
length: 235,
severity: 'ERROR',
code: '0A000',
detail: undefined,
hint: 'Use a BEGIN block with an EXCEPTION clause instead.',
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: 'PL/pgSQL function dugong.quizquestion_jsadd(integer,json) line 20 at SQL statement',
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'pl_exec.c',
line: '3377',
routine: 'exec_stmt_execsql' }
尝试&#39; b&#39;案件: Console.log的&#39; b&#39;情况下:
pg receive :
[ 8,
[ { order: 1, question_id: 5 },
{ order: 2, question_id: 2 },
{ order: 3, question_id: 3 },
{ order: 4, question_id: 1 } ] ]
&#39; b&#39;错误案例:
{ [error: invalid input syntax for type json]
name: 'error',
length: 178,
severity: 'ERROR',
code: '22P02',
detail: 'Expected ":", but 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' }
这是节点中的功能。
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 i = 0 ; i < array.length ; i++){
console.log(typeof(array[i]));
if(i == array.length -1){
callback(array);
}
}
};
exports.test2 = function(array,callback){
var a = 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 the function');
});
});
};
这是表结构。
CREATE TABLE Dugong.QuizQuestion(
Quiz_ID int,
Order_question int,
Question_ID int, --FK
LastModified timestamp with 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)
);
我的pg功能:
CREATE OR REPLACE FUNCTION Dugong.QuizQuestion_JsAdd(
in_quiz_id int,
in_question_id_list json
)
RETURNS boolean AS $$
DECLARE
idx integer;
i json;
BEGIN
idx := 1;
FOR i IN SELECT * FROM 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 THEN
RAISE NOTICE 'The transaction is in an uncommitable state.'
'Transaction was rolled back.';
RAISE NOTICE '% %', SQLERRM, SQLSTATE;
ROLLBACK;
END;
$$ LANGUAGE PLPGSQL;