$.each( model.getRows() TypeError: Cannot read property 'each' of undefined

  • 1
  • Question
  • Updated 1 month ago
What am I doing wrong? 

I have a table full of Proposal records. Each Proposal has a child record that we call a Commit. I want to let a user select some or all Proposals, enter a couple dates in a popup, and update those dates on the child Commit record of each selected Proposal.

The popup has a field editor with two date fields from a Ui-only model, and a title component with a button that runs the following snippet. 
var table = skuid.$('#vwProposalTableFull'),
    list,
    datesModel =  skuid.$M('vwCommitUpdates_UiOnly'); // the ui-only model that accepts new dates from the user
datesModel.save(); 
var datesRow = datesModel.getFirstRow(),
    sentDate = datesModel.getFieldValue(datesRow,'Award_Letter_Sent_Ui'),
    signedDate = datesModel.getFieldValue(datesRow,'Award_Letter_Signed_Ui');
// STEP 1: Load array with IDs of selected Proposals from the table. Funded only.
// This step works correctly.
if (table.length) {
    
    list = table.data('object').list;
    
}   
var idsArray = skuid.$.map(list.getSelectedItems(), function(item) { 
    
    if (item.row.Status__c == "Funded") {
    
        return item.row.Id;
    
    }
    
});
// STEP 2: Set condition to get only the Commits that belong to selected Proposals
// This step works correctly
var model = skuid.$M('vwCommitUpdates'),
    condition = model.getConditionByName('Proposal_Details__c');
model.setCondition(condition, idsArray, false);  
// STEP 3: Query model. When query completes, callback loops through each row to update date fields.
// This step is not working correctly. 
model.updateData( function() {
    
    try {
        $.each(model.data, function(i, row) {
            //if(row.Award_letter_sent__c ... I will set conditions here, when I get it working ) {
                    
                model.updateRow(row, 'Award_letter_sent__c', sentDate);
                model.updateRow(row, 'Award_Letter_Signed__c', signedDate);
        
            //}
            
        });
    
    } catch(err) {
  
        console.log('error! ... ' + err); // Result is always: "TypeError: Cannot read property 'each' of undefined"
  
    } finally {
    
        console.log('code in finally');
    
    }
    
    model.save();
});
// STEP 4: housekeeping (close popup etc)
skuid.$('.ui-dialog-content').dialog('close');

Steps 1, 2, and 4 work correctly. I cannot get Step 3 to work. The result of my try/catch is always err: "TypeError: Cannot read property 'each' of undefined".

When I step through this code in the console, or use console.log to show the state of vars in each block, I see rows in model.

This syntax seems to obey the documentation here, https://docs.skuid.com/latest/en/skuid/api/skuid_model_model.html#skuid.model.Model.updateRows

I've tried the following variants in my try{} block, the result is the same TypeError: Cannot read property 'each' of undefined.

Variant 1:
var rowsToUpdate = {};

$.each( model.getRows(), function() {

    rowsToUpdate[this.Id] = {

        Award_letter_sent__c: sentDate,
        Award_Letter_Signed__c: signedDate     }; }); 
model.updateRows( rowsToUpdate );

Variant 2:
var rows [];
rows = model.getRows();
$.each(rows, function(row) {

model.updateRow(row, {
Award_letter_sent__c: sentDate,
Award_Letter_Signed__c: signedDate
}); });
Photo of Tim Shores

Tim Shores

  • 660 Points 500 badge 2x thumb
  • flummoxed

Posted 1 month ago

  • 1
Photo of Mike Dwyer

Mike Dwyer, Champion

  • 3,790 Points 3k badge 2x thumb
Do you just need to use skuid.$.each?
Photo of Tim Shores

Tim Shores

  • 660 Points 500 badge 2x thumb
Wow! Look at that. I must have deleted my var assignment of $ = skuid.$. Thank you for rescuing me from my deep well of over-thinking.
Photo of Mike Dwyer

Mike Dwyer, Champion

  • 3,790 Points 3k badge 2x thumb
Yep. I noticed the skuid.$ elsewhere in the code. But I had to actually look up the reason for assigning the dollarSign to skuid.$.