Unable to use false from query in createIf.

I can now get the information from the query into the state but it's not evaluating to false in the createIf. Here's where I'm currently at:

alterState((state) => {
  console.log("Altering1...");
  const idCards = state.data.form.packets;
  state.data.form.packets = [12];
  var highestIndex = 0;
    [1,2,3,4,5].forEach(function(item, index, array) {
      var packetSize = dataValue("form.recapitulatif_.packet"+ item +"-size")(state);
      if( typeof packetSize !== "undefined" && packetSize !== "" && typeof packetSize !== 0 && packetSize !== null) {
        console.log("Packet Size", packetSize);
        state.data.form.packets[highestIndex] = {
        savingGoalInsertName: dataValue("form.client_id")(state) + "-2019-" + String(dataValue("form.recapitulatif_.packet"+ item +"-product_name")(state)).replace(/\+/g,"%2B"),
        relatedSavingGoal: dataValue("form.client_id")(state) + "-2019-" + String(dataValue("form.recapitulatif_.packet"+ item +"-product_name")(state)),
        productId: dataValue("form.recapitulatif_.packet"+ item +"-product_id")(state),
        pricebook: dataValue("form.recapitulatif_.packet"+ item +"-pricebook_id")(state),
        packetSize: dataValue("form.recapitulatif_.packet"+ item +"-size")(state),
        product_type_id: dataValue("form.recapitulatif_.packet"+ item +"-product_type_id")(state)
      };
      console.log("index", state.data.form.packets[highestIndex] );
      highestIndex += 1;
    } else {
      console.log("Undefined-Packet-Size for item: ", item);
    }
  });
  return state;
})(state),

alterState((state) => {
  console.log("Altering2...");
  return query("Select Id, Client__c, Program__c , Membership_End_Date__c from Program_Membership__c where Program__c = 'a2H1j0000008Tt6EAE' and Client__c = null")(state);
}),

alterState((state) => {
  state.data.form.isProgramMembership = state.references[0].totalSize !== 0;
  return state;
});

createIf(dataValue("form.isProgramMembership") , "Program_Membership__c", fields(
  relationship("Client__r","Name", dataValue("form.isProgramMembership") ),
  field("Program__c", dataValue("form.program_id")),
  field("Membership_Start_Date__c",dataValue("form.start_date"))));

combine(function(state) {
  if (dataValue("form.meta.username")(state) != "kamara"
    & dataValue("form.meta.username")(state) != "eyram"
    & dataValue("form.meta.username")(state) != "noella") {
    upsert("Client__c","Name", fields(
      field("Name", dataValue("form.client_id")(state)),
      field("First_Name__c", dataValue("form.demographic_infoss.first_name")(state)),
      field("Last_Name__c", dataValue("form.demographic_infoss.last_name")),
      field("Sex__c", dataValue("form.demographic_infoss.gender")),
      field("Village_Object__c", dataValue("form.village_sf_id"))
    ))(state),
    beta.each(
      dataPath("form.packets[*]"),
      upsert("Savings_Goal__c","Name", fields(
        relationship("Client__r", "Name", dataValue("form.client_id")(state)),
        field("Year__c", "2019"),
        field("Price_Book__c",dataValue("pricebook")),
        field("Name", dataValue("savingGoalInsertName"))
      ))
    )(state),
    beta.each(
      dataPath("form.packets[*]"),
      create("Goal_Items__c", fields(
        relationship("Savings_Goal__r", "Name",dataValue("relatedSavingGoal")),
        field("Product_Type__c", dataValue("product_type_id")),
        field("Product__c", dataValue("productId")),
        field("Desired_Units__c", dataValue("packetSize"))
      ))
    )(state);
  }
});

If I pass in state after the first alter state function it says "Cannot read property query of undefined", if I pass it into the second it says "Cannot read property zero of undefined".

As a test, I included the value 'form.isProgramMembership' on line 41 and it is evaluating to false when there is zero rows returned.

Thank you for your help.

Hey Patrick,

Not a problem—this smells like a timing issue with promise returns. I haven’t looked at the createIf for quite some time, but I know that you could control the timing of execution by using another generic function. Like this:

alterState(state => {
return query(
SELECT Field__c FROM Object__c WHERE Ext_ID__c = '1'
)(state)
})

alterState(state => {
console.log(state.references);
return createIf(
// logical test…
state.references[0].totalSize > 0,
// sObject…
‘Contact’,
// params…
fields(
field(‘Name’, ‘Taylor’)
)
)(state)
});

In your specific case, try wrapping your createIf(…) function in alterState:
alterState(state => {
return createIf(…)(state)
});

Or, using shorthand:
alterState(state => (
createIf)(state)
);

Please let me know if this works. (It did for me, using the test code at the top of this response.) If so, it’s safe to say that createIf is not well designed (probably by me!) and that we should adjust it so that (a) it doesn’t get executed until after state is returned from the previous operation and (b) you can use either a simple logical test or a function that returns true/false.

Best,
Taylor