I try to flatten the query result, But it always gives me the error. Here is my Array
responseValue.listAll:enter image description here

My lightning controller:

var listAfterFlatten = [];
            responseValue.listAll.forEach(function (course) {
                console.log("Here is result", course);
                var flattened = this.flattenQueryResult(course);
                listAfterFlatten.push(flattened);

            });
            component.set("v.coursesList", listAfterFlatten);

But when I check the console, it gives me the list what I want. enter image description here Here is my flattenQueryResult :

flattenQueryResult :function(data) {
        var result = {};
        function recurse (cur, prop) {
            if (Object(cur) !== cur) {
                result[prop] = cur;
            } else if (Array.isArray(cur)) {
                for(var i=0, l=cur.length; i<l; i++)
                    recurse(cur[i], prop + "[" + i + "]");
                if (l == 0)
                    result[prop] = [];
            } else {
                var isEmpty = true;
                for (var p in cur) {
                    isEmpty = false;
                    recurse(cur[p], prop ? prop+"."+p : p);
                }
                if (isEmpty && prop)
                    result[prop] = {};
            }
        }
        recurse(data, "");
        return result;
    }

I don't know why it's undefined. Could someone helps me? Thanks

up vote 1 down vote accepted

If the error is "Cannot read property 'flattenQueryResult' of undefined" that simply means that JavaScript can't find a function called 'flattenQueryResult'. If you put your 'flattenQueryResult' function in the JavaScript helper you can call it by writing:

var flattened = helper.flattenQueryResult(course);

EDIT

Inside the anonymous function passed as argument to the forEach function the this keyword doesn't refer to the helper, the context is different. You can try something like this:

        var myFlattenFunction = this.flattenQueryResult;
        var listAfterFlatten = [];
        responseValue.listAll.forEach(function (course) {
            console.log("Here is result", course);
            var flattened = myFlattenFunction(course);
            listAfterFlatten.push(flattened);

        });
        component.set("v.coursesList", listAfterFlatten);

Or you can pass the helper object in the argument of your helper function, something like:

yourHelperFunction: function(helper) {
...
var flattened = helper.flattenQueryResult(course);
...
}
  • It's already in Helper class. That's why I use this.flattenQueryResult(course); – user7697691 Sep 14 at 12:21
  • I've just edited my answer – AlfaCold Sep 14 at 12:33

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.