Search code examples
jsontypescriptnestedsequelize.jshapi

Hapi response doesn't show tree-like (nested) json


Ok, so I have an object that has the following structure:

interface FolderWithContent {
    uuid: string
    name: string;
    folders: Array<FolderWithContent>;
    files: Array<Files>;
}

Where Files is an extension of Sequelize.Model.

And I'm trying to return it with hapi (return h.response(tree).code(200);) (tree is my object, of course)

The thing is, my object has several levels and the response is only showing the root and 1st level. So if I have

{
    "name": "folder1.1",
    "uuid": "1",
    "folders": [
        {
            "name": "folder2",
            "uuid": "3986b8ca-314c-4ba8-b47c-9baa29ca7adc"
        },
        {
            "name": "folder2.6",
            "uuid": "7ff93401-1281-419c-9541-fb859c4e79e1",
            "folders": [
                {
                    "name": "folder3.1",
                    "uuid": "8d76aa76-fa42-40c6-9c46-9fa26c6b555c"
                }
            ],
            "files": [
                {
                    "name": "file5",
                    "uuid": "9a8c9aa2-23bd-45e3-bb43-ddf0e085b066"
                }
            ]
        }
    ],
    "files": [
        {
            "name": "file2.2.2",
            "uuid": "88519cec-b19a-4e12-9138-6273ac66ba76"
        },
        {
            "name": "file1",
            "uuid": "9eb5235d-9d04-494d-845c-4a9780bc9687"
        }
    ]
}

I will not get folders and files inside of folder2.6. I have tried to return tree.folders[2], but it still only shows the folder name and uuid. However, if i return tree.folders[2].folders, only then it shows me the folders and files inside folder2.6.

I have tried calling Json.stringfy(tree) but it also has the exact same problem.


Solution

  • I still don't know WHY I had the problem, but I'm posting the way I found to fix it.

    Turns out the issue was with sequelize. All the objects were models from sequelize. Converting the objects to simple JSONs did it for me.

    tree.folders = content.folders?.map((folder) => {
            return {
                name: folder.name,
                uuid: folder.uuid,
                updated_at: folder.updated_at,
                synced: folder.synced,
                folders: [],
                files: []
            };
        });
        tree.files = content.files?.map((file) => {
            return {
                name: file.name,
                uuid: file.uuid,
                updated_at: file.updated_at,
                synced: file.synced
            };
        });