Skip to content

Saving nested JSON object results in flattened entry #5

@Anima-t3d

Description

@Anima-t3d

Thanks for providing this useful tool. I have been trying to use it in the past few days and I encounter a specific issue.

Original db.json state:

"widgets": [
    {
      "dashboardId": 1,
      "id": "WidgetA001",
      "permissions": {
        "canCollapse": true,
        "canHide": true
      },
      "config": {
        "isHidden": false,
        "isCollapsed": true
      }
    }
]

Then call the following:

onSave(widget) {
    let foundIndex = this.state.data.findIndex(function (item) {
      return item.id === widget.id;
    });

    let modifiedWidget = this.state.data[foundIndex];
    modifiedWidget.config.isCollapsed = false;
    console.log('modify config collapse after', modifiedWidget)

    $.ajax({
      url: 'http://localhost:3000/widgets/' + widget.id,
      dataType: 'json',
      cache: false,
      method: 'PUT',
      data: modifiedWidget,
      success: function(data) {
     console.log('data loaded in succes update onSave', data)
      }.bind(this),
      error: function(xhr, status, err) {
        console.error('loading in parent', status, err.toString());
      }.bind(this)
    });
  }

Expected db.json state:

"widgets": [
    {
      "dashboardId": 1,
      "id": "WidgetA001",
      "permissions": {
        "canCollapse": true,
        "canHide": true
      },
      "config": {
        "isHidden": false,
        "isCollapsed": false //<--
      }
    }
]

Actual db.json state:

"widgets": [
    {
      "dashboardId": 1,
      "id": "WidgetA001",
      "permissions": {
        "canCollapse": true,
        "canHide": true
      },
      "config": {
        "isHidden": false,
        "isCollapsed": true //<--
      },
      "permissions[canCollapse]": true,
      "permissions[canHide]": true,
      "config[isHidden]": false,
      "config[isCollapsed]": false
    }
]

So instead of properly updating it in a nested object, I get a flattened object. Did I do something wrong? Any help would be greatly appreciated. Up until it hits thedb.json file it preserves the correct format (in the console.log)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions