0

I have a javascript object stored in variable dashboard_filter_value_attributes which hold the tableau filter names which I am trying to send via post to my rails controller for further processing.

when i do console.log(dashboard_filter_value_attributes), i see the below output on the console, i have expanded the 0th index key that why you see __proto__: Object in the below output

{}
0:
filter_name: "Filter One"
__proto__: Object
1: {filter_name: "Filter Two"}
2: {filter_name: "Filter Theree"}
3: {filter_name: "Filter Four"}
4: {filter_name: "Filter Five"}
5: {filter_name: "Filter Six"}
6: {filter_name: "Filter Seven"}
7: {filter_name: "Filter Eight"}
8: {filter_name: "Filter Nine"}
9: {filter_name: "Filter Ten"}
__proto__: Object

I have defined let dashboard_filter_value_attributes = {};

but I am unable to send the object via post. The object is not getting passed and is ingnored during the jQuery.post execution, i also do not see any error on the console.

Here is my jQuery.post call

let activeSheet = tableauViz.getWorkbook().getActiveSheet();
let viewName = 'my custom view';
let counter = 0;
let dashboard_filter_value_attributes = {};


activeSheet.getWorksheets().forEach(function(worksheet) {
  if (worksheet.getName() == "Filter") {
    worksheet.getFiltersAsync().then(function(filterParameterList) {
      filterParameterList.forEach(function(fp) {
        const name = filter ? fp.getFieldName() : fp.getName();
        dashboard_filter_value_attributes[counter] = {
          filter_name: name,
        };
        counter =  counter + 1;
      }
    });
  });
});

jQuery.post($('#new_dashboard_filter').attr('action'), {
  dashboard_filter: {
    name:viewName,
    dashboard_filter_values: dashboard_filter_value_attributes
  }
});

I have also tried to use JSON.strigify but even then I am not able to post the object to my rails controller.

When i use console.log(JSON.strigify(dashboard_filter_value_attributes)); i see []

Any help in this would be really great. Thanks.

opensource-developer
  • 2,826
  • 4
  • 38
  • 88
  • it seens your dashboard_filter_value_attributes is an empty array – Ariel Batista Oct 01 '19 at 13:44
  • @ArielBatista, i have posted what i see on console when i print `dashboard_filter_value_attributes ` – opensource-developer Oct 01 '19 at 13:48
  • _"Any help in this..."_ - For this you will have to add a [mcve]. Otherwise we can only guess (and one of mine would be that your "array" looks fishy) – Andreas Oct 01 '19 at 13:51
  • @Andreas, sure i will try to do that, the object `dashboard_filter_value_attributes` hold array of other objects and in the question i have tried to expand one of the keys, thats why you see `__proto__: Object` – opensource-developer Oct 01 '19 at 13:54
  • 1
    _"When i use console.log(JSON.strigify(dashboard_filter_value_attributes)); i see []"_ - Which would not be possible if you didn't change the content of `dashboard_filter_value_attributes` because you're assigning it an empty object and not an array -> [mcve] – Andreas Oct 01 '19 at 14:04
  • @Andreas, I have updated the question with how i am constructing the `dashboard_filter_value_attributes` params. – opensource-developer Oct 01 '19 at 14:10
  • where are you define the `dashboard_filter_value_attributes`, can you update the code? – Terence Cheng Oct 01 '19 at 14:17
  • 2
    And with that we're back at the famous -> [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Oct 01 '19 at 14:18
  • ah, @Andreas, thanks for pointing, my bad, should have realised it, but many thanks for the help :) – opensource-developer Oct 01 '19 at 14:20

1 Answers1

0

It may be in the method used to define your Array/Object

I have defined let dashboard_filter_value_attributes = {};

This defines an empty Object, but then you have filled it with items with numeric keys (0-9), which is what an Array is typically used for. Of course Arrays are Objects in JavaScript but Objects typically have named keys (filter_name_1 or something).

The short form to define an Array in JavaScript is [], not {}

scripter
  • 75
  • 2
  • 12