Reputation: 899
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?
Upvotes: 0
Views: 13427
Reputation: 899
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.
Upvotes: 0
Reputation: 1831
As you pointed out in your question, clientsRelationName is a string corresponding to the name of the relation. Your actual relation is just Clients, therefore either of the following should work:
record[Clients] = clients;
or
record.Clients = clients;
I would actually suggest using record.YourRelation because when you use the dot after your record the intellisense will automatically bring up all options for field names or relation end names that are available with that record.
Upvotes: 0
Reputation: 11
I have seen this issue where using Object.keys()
on a server-side record yields [key, data, state]
instead of the expected fields for that record. So if your programmatic assignment involves iterating on the properties of that record object, you may hit this data
property.
Unfortunately that's all I know so far. Maybe the App Maker Team can provide further insight.
Upvotes: 1