Search code examples
google-app-maker

TypeError: Cannot read property "data" from undefined


There is a many to many relationship between two records namely countries and clients. When I fetch some records from the clients ( an array of clients ) and I try to assign them problematically to a country( record ) like this record[clientsRelationName] = clients I get the following bazaar error, TypeError: Cannot read property "data" from undefined. I know for sure that the variable clientsRelationName is actually a string that corresponds to the name of the relation which is simply just called clients. And it has nothing to do with a variable called data. In fact data does't exist. And I know for sure that record is a defined variable.

Any idea why this is happening? Is it a bug?


Solution

  • After a lot of trail and error, I finally found a way to make it work using a rather very simple solution and its the only way I could make it work. Basically to a void getting this strange error when when modifying an association on a record TypeError: Cannot read property "data" from undefined , I did the following:

    Loop through the record relation(array) and and popup every record in side it. Then loop through the other records that you want to assign the record relation to ( modify the association ) pushing every element to the record relation.

    var length = record[relationNameAsVariable].length;
    for(var i=0; i<length; i++){
      record[relationNameAsVariable].pop();
    }
    

    now record[relationNameAsVariable] is empty so do the following:

    for(var i=0; i < clientsArray.length; i++ ){
      record[relationNameAsVariable].push(clientsArray[i]);
    }
    

    It could be a bug or something else that I'm doing wrong when trying to replace the whole association. I'm not sure. But this works like a champ.