![](/img/trans.png)
[英]how to clear previous data from variable if i don't know the type of variable(may be array,object or primitive)
[英]I want to insert an array of type array into postgress but I don't know how to do it from node
query.callfunction('fn_create_mp_product', parameters, (err, result) => {
if (err) {
console.log(err)
callback(err);
} else {
if (result.status == 'success') {
callback(null, result);
} else {
callback(result.message);
}
}
});
我想将参数传递给 fn_create_mp_product function 在参数上的参数一个是我想从节点插入的数组类型的数组
var products='array['+data.productId+']';
所以我尝试了这样,但它遇到了一个错误
{ error: function fn_create_mp_product(unknown, unknown, unknown, unknown) does not exist
at Connection.parseE (/home/alignminds/Desktop/insidersBackendgit/insiderbackend/node_modules/pg/lib/connection.js:614:13)
at Connection.parseMessage (/home/alignminds/Desktop/insidersBackendgit/insiderbackend/node_modules/pg/lib/connection.js:413:19)
at Socket.<anonymous> (/home/alignminds/Desktop/insidersBackendgit/insiderbackend/node_modules/pg/lib/connection.js:129:22)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'error',
length: 249,
severity: 'ERROR',
code: '42883',
detail: undefined,
hint:
'No function matches the given name and argument types. You might need to add explicit type casts.',
position: '15',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_func.c',
我只希望此格式参数中的array[23,23]
传递给我的函数我能做什么
如果您想将可变数量的参数作为数组传递给 Postgres function,或者您传递给 function 的一个(或多个)参数是一个可变长度的数组,那么您的问题并不清楚。
第一个风景是不可能的:Postgres function 参数非常严格,它们在 function 创建时一劳永逸地声明。
第二种是可能的。 假设你有这张桌子
create table tbl (
arr_col integer[] null
)
以及在表中插入值的 function
CREATE OR REPLACE FUNCTION public.inserter(arr_value int[])
RETURNS void
LANGUAGE sql
AS $function$
insert into tbl (arr_col)
select arr_value;
$function$
;
那么你可以像这样使用 function
select * from inserter(array[1,2])
插入的数组可以有任何长度,但它必须只有一个维度(例如,不是矩阵),这是因为从表创建开始,维度不能改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.