Loving the new collections feature! I’m having a bit of a problem figuring out the correct way to accomplish my goal:
Adding to the state:
fn((state) => {
state.newItems = ["one", "two", "three"];
console.log(state);
return state;
});
gives this state object:
{
"references": [],
"data": {},
"configuration": {
"collections_endpoint": "****",
"collections_token": "****",
"access_token": "****",
"accessToken": "****"
},
"newItems": [
"one",
"two",
"three"
]
}
I’m trying to pass newItems as a parameter into the set() function and having mixed results:
collections.set(collectionName, itemName, $.newItems);
and
collections.set(collectionName, itemName, (state) => state.newItems);
each throw an error (truncated for brevity):
# Postgrex.Error at POST /collections/test-collection\n\nException:\n\n ** (Postgrex.Error) ERROR 21000 (cardinality_violation) ON CONFLICT DO UPDATE command cannot affect row a second time\n \n hint: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.\n
Encompassing the array seems to work in this example, but in my greater implementation, it leads to a nested array and doesn’t feel right!
collections.set(collectionName, itemName, [$.newItems]);
Also, I expected this to work; it executes but doesn’t do anything:
fnIf(
(state) => Array.isArray(state.newItems),
(state) => {
collections.set(collectionName, itemName, state.newItems);
return state;
}
);
When troubleshooting, I experimented and added a couple console.log inside the collections.js file set function, recompiled, and found that:
export function set(name, keyGen, values) {
let argCount = arguments.length;
console.log("hit"); // this line shows in the console
return async state => {
console.log("doesn't hit"); // this line does not show in the console
if (argCount < 3) {
Ultimately, my question is, what’s the correct way to conditionally add an array of strings using collections.set() ?
Thank you!