I am trying to insert multiple 9 columned datasets into postgresdb and am struggling to get it to work properly
function issue_formatter(issue_check_v, issue_v) {
if ((issue_check_v.value == true) && (issue_v.value !== null)) {
return issue_v.value;
} else {
return "";
}
}
const array = [];
var source_id = {{currentProduct.data.doc_id[0]}};
var doc_id = {{Matches_from_code.data.id}};
var var_doc_id = {{Variants_from_code.data.id}};
var url = {{Matches_from_code.data.url}};
var var_url = {{Variants_from_code.data.url}};
var operator_name = {{current_user.fullName}};
var match_type = {{text24}};
var var_match_type = {{text32}};
var match_code = {{text33.value}};
var issue_check = {{Issue_check}};
var var_issue_check = {{Issue_checker_var}};
var issue = {{issue_selecter}};
var var_issue = {{issue_selecter2}};
var comment_select = {{select1.value}};
var custom = {{textInput1.value}};
var len = {{Matches_from_code.data.doc_id.length}};
var var_len = {{Variants_from_code.data.doc_id.length}}
const tempArray = ["source_id", "doc_id", "url", "time_inserted", "operator_name", "operator_comment", "source_comment", "match_code", "match_type"];
array.push(tempArray);
var comment;
if (comment_select != null) {
if ((comment_select == "Custom") && (custom != "")){
comment = custom;
}
else if (comment_select != "Custom"){
comment = comment_select;
} else {
comment = "";
}
}
// match data
for (var i = 0; i < len; i++) {
let dataRow = [
source_id,
doc_id[i],
url[i],
"NOW()",
operator_name,
issue_formatter(issue_check[i], issue[i]),
comment,
match_code,
match_type[i].value
];
array.push(dataRow);
}
// var data
for (var i = 0; i < var_len; i++) {
let dataRow = [
source_id,
var_doc_id[i],
var_url[i],
"NOW()",
operator_name,
issue_formatter(var_issue_check[i], var_issue[i]),
comment,
match_code,
(var_match_type[i].value).replace(/\*/g, '')
];
array.push(dataRow);
}
return array;
what am I doing wrong?
Yes, you should map your return data as array of object
, you should not push the column(in your code is tempArray in to array.
The data should return as format as [{"source_id":0,"docs_id":0},{"source_id":1,"docs_id":1}]
Here is my modified code for your referrence.
function issue_formatter(issue_check_v, issue_v) {
if ((issue_check_v.value == true) && (issue_v.value !== null)) {
return issue_v.value;
} else {
return "";
}
}
const array = [];
var source_id = {{currentProduct.data.doc_id[0]}};
var doc_id = {{Matches_from_code.data.id}};
var var_doc_id = {{Variants_from_code.data.id}};
var url = {{Matches_from_code.data.url}};
var var_url = {{Variants_from_code.data.url}};
var operator_name = {{current_user.fullName}};
var match_type = {{text24}};
var var_match_type = {{text32}};
var match_code = {{text33.value}};
var issue_check = {{Issue_check}};
var var_issue_check = {{Issue_checker_var}};
var issue = {{issue_selecter}};
var var_issue = {{issue_selecter2}};
var comment_select = {{select1.value}};
var custom = {{textInput1.value}};
var len = {{Matches_from_code.data.doc_id.length}};
var var_len = {{Variants_from_code.data.doc_id.length}}
const tempArray = ["source_id", "doc_id", "url", "time_inserted", "operator_name", "operator_comment", "source_comment", "match_code", "match_type"];
// array.push(tempArray); delete this
var comment;
if (comment_select != null) {
if ((comment_select == "Custom") && (custom != "")){
comment = custom;
}
else if (comment_select != "Custom"){
comment = comment_select;
} else {
comment = "";
}
}
// match data
for (var i = 0; i < len; i++) {
let dataRow = [
source_id,
doc_id[i],
url[i],
"NOW()",
operator_name,
issue_formatter(issue_check[i], issue[i]),
comment,
match_code,
match_type[i].value
];
array.push(dataRow);
}
// var data
for (var i = 0; i < var_len; i++) {
let dataRow = [
source_id,
var_doc_id[i],
var_url[i],
"NOW()",
operator_name,
issue_formatter(var_issue_check[i], var_issue[i]),
comment,
match_code,
(var_match_type[i].value).replace(/\*/g, '')
];
array.push(dataRow);
}
// new add code here
let result = array.map((item,index)=>({[tempArray[index]]:item[index]}))
return result;
1 Like