Map String into ColumnSet #937
-
Hello I have an array of strings that I need to map into a ColumnSet manually:
I am trying to go through these and create a ColumnSet
This will pass the compiler but unfortunately when it runs the insert statement it will ONLY access the last property for each object which is "Warning." I have tried to clone the string value and use a for loop instead of mapping. I can see when I print the ColumnSet that each name is correct too.
Any help is much appreciated on how to access the correct property when mapping the ColumnSet from a string array of names |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
There is something odd in your example about the let insertColumnSet = new pgp.helpers.ColumnSet(
tableConfig.columns.map((column, idx) => {
return {
name: column,
prop: "_" + idx,
init: (col) => {
return col.source[column]
}
}
}),
{table: tableConfig.tableName}
); I will look at it later, to figure out why |
Beta Was this translation helpful? Give feedback.
-
So I have done some investigation, since I did the formatting logic years ago, and started forgetting it myself.
In your specific case, And so you end up using some random, but importantly unique property names, and then the whole thing works. None of this is ever necessary when you either use proper valid column names or at least simple property references, through re-mapping, like shown below: const updatedData = someData.map(a => ({
accountName: a['Account Name'],
accountNumber: a['Account Number']
})); And then inside your const insertColumnSet = new pgp.helpers.ColumnSet([
{
name: 'Account Name',
prop: 'accountName'
},
{
name: 'Account Number',
prop: 'accountNumber'
}
],
{table: tableConfig.tableName}); And that's it. |
Beta Was this translation helpful? Give feedback.
There is something odd in your example about the
prop
usage. Making it the same for all columns confuses the parser. If you make it unique, then it works:I will look at it later, to figure out why
prop
is confused in this case. You have y…