Javascript Action - Iterate List of Lists (MxObject[] inside another MxObject[])

0
Hi Team, I have MxObject[] called rowList which contains another MxObject[] called valueList[]. I am able to iterate the rowList and got the valueList. var valueList = [];     for(var i=0;i<rowList.length;i++){         valueList = rowList[i].get("CompareDOE.row_Value")     } I am not able to Iterate the valueList as it is printing the GUIDs only. Can you please provide me an example to iterate valueList[] and get the values.   Domain Model:  
asked
1 answers
6

According to the documentation you can use mx.data.get to retrieve objects https://apidocs.mendix.com/7/client/mx.data.html#.get

var valueList = [];

    for(var i=0;i<rowList.length;i++){
        mx.data.get({
           guid: rowList[i].getGuid(),
           path: "CompareDOE.row_Value",
           callback: function (values) {
               // do something with values
           },
           error: function (error ) {
                console.error(error);
          }
        });
    }

To wait to retrieve all values you can use Promise.all
 

function fetchRowValuesAsPromise(row) {
 return new Promise(function (resolve, reject) {
        mx.data.get({
           guid: rowList[i].getGuid(),
           path: "CompareDOE.row_Value",
           callback: function (values) {
               resolve(values);
           },
           error: function (error ) {
               reject(error);
          }
        });
  });
}

var valueList = [];
Promise.all(rowList.map(fetchRowValuesAsPromise))
.then(function (valueInValueList) {
   // valueInValueList = [[1,2,3],[4,5,6],[7,8,9]]
}).catch(function (err) {
   console.error(err);
};

 

answered