【发布时间】:2019-06-21 04:52:00
【问题描述】:
在使用 ColumnSet 中列的 initCB 属性导入数据时,我尝试拆分记录集中的子对象。
但是当我对两个不同的目的地名称但一个来源使用两个不同的初始化函数时,我得到了相同的结果。
const cs = new pgp.helpers.ColumnSet([
'id',
{ name: 'source_id', prop: 'source', init: function(obj) { return obj.value.id; } },
{ name: 'source_name', prop: 'source', init: function(obj) { return obj.value.name; } },
], { table: 'test_table' });
const data = [
{ id: 1, source: { id: 1, name: 'source1' } },
{ id: 2, source: { id: 1, name: 'source1' } },
{ id: 3, source: { id: 2, name: 'source2' } },
];
const insert = pgp.helpers.insert(data, cs);
结果是:
INSERT INTO "test_table"("id","source_id","source_name") VALUES
(1,'source1','source1'),
(2,'source1','source1'),
(3,'source2','source2')
而不是预期:
INSERT INTO "test_table"("id","source_id","source_name") VALUES
(1,1,'source1'),
(2,1,'source1'),
(3,2,'source2')
似乎第二次调用相同源字段的回调函数覆盖了先前在此源字段上调用另一个回调函数的结果。
如何避免这种情况? 或者在导入过程中有另一种拆分子对象的方法?
【问题讨论】:
标签: node.js pg-promise