Oddities using new collections feature when passing state value

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!

Hi Jason,

Thanks for the report! I’m looking into this now

Hi Jason

It’s my code that’s wrong, not yours!

We had a little issue on collections.set when a function is passed as the third argument.

This should all be fixed in collections 6.0.1 (restarting your lightning worker will auto-update and you’ll see the version number in your logs)

1 Like

This is great. Thanks for confirming. I tested the change and it’s working as expected.

1 Like