Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to visualize response after update latest update #13391

Closed
1 task done
aksevenli opened this issue Jan 9, 2025 · 9 comments
Closed
1 task done

Unable to visualize response after update latest update #13391

aksevenli opened this issue Jan 9, 2025 · 9 comments

Comments

@aksevenli
Copy link

aksevenli commented Jan 9, 2025

Is there an existing issue for this?

  • I have searched the tracker for existing similar issues and I know that duplicates will be closed

Describe the Issue

After updating to the latest version of Postman (v11.27.3), I am encountering an issue with the Visualizer feature in the “post-response” in the Scripts tab. Specifically, the pm object, which is essential for Postman-specific scripting, is throwing a ReferenceError: pm is not defined. This error occurs when trying to execute a script for a custom visualization of API responses.

My response has a unique structure, so I can’t directly use post-bot to generate visualization.

This is my script for the visualization, which worked for the older version:

`
var template =

<style> .fill, body, html { height: 100%; } table, td, th { border: 1px solid gray; } /* Additions for table cell and header styling */ .my-table th, .my-table td { max-width: 120px; padding: 5px; border: 1px solid #ddd; text-align: left; vertical-align: top; } /* Specific styling for table headers */ .my-table th, .my-table td { white-space: normal; word-wrap: break-word; } /* Reduce the font size for all table content */ .my-table, .my-table th, .my-table td { font-size: 0.8em; } .my-table th { position: sticky; top: 0; background-color: #f0f0f0; z-index: 1; } table { table-layout: fixed; width: 100%; } /* Alternate row coloring */ .my-table tr:nth-child(odd) { background-color: #f9f9f9; /* Light grey for odd rows */ } .my-table tr:nth-child(even) { background-color: #ffffff; /* White for even rows */ } *, html { font-family: Verdana, Arial, Helvetica, sans-serif; } form, h1, h2, h3, h4, h5, li, p, ul { margin: 0; padding: 0; } .container { width: 100%; max-width: none !important; } /* Additional CSS for key-value pairs */ .key-value-pair { background-color: #e8e8e8; /* Light gray background */ border: 1px solid #ccc; /* Light border for distinction */ border-radius: 4px; /* Rounded corners */ padding: 5px; /* Some padding around the text */ margin: 5px 0; /* Margin for spacing between pairs */ font-family: monospace; /* Monospaced font for better readability */ } /* Style adjustments for better spacing and alignment */ .key-value-pair::before { display: inline-block; margin-right: 5px; color: #333; } </style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script> $(function () { pm.getData((err, data) => { if (err) { console.error("Error getting data:", err); $("#container").text("Error getting data."); return; }
            // Clear previous contents
            $("#container").empty();

            // Check if the outputs array is present
            if (Array.isArray(data.json.outputs)) {
                data.json.outputs.forEach((item) => {
                    // Check for items with a structure applicable for table rendering
                    if (
                        item.value &&
                        Array.isArray(item.value) &&
                        item.value.some((val) => val.metadata || val.data)
                    ) {
                        const tableData = transformDatagridData(item.value);
                        $("#container").append($("<h4>").text(item.name)); // Add a header for each table
                        $("#container").append(buildTable(tableData));
                    } else {
                        // For other items, display them as key-value pairs
                        $("#container").append(
                            $('<div class="key-value-pair">').append($("<span>").text(item.name)).append(": " + item.value)
                            
                        );
                    }
                });
            } else {
                $("#container").text(
                    "The data is not an array as expected."
                );
            }
        });
    });

    function transformDatagridData(value) {
        // Assume value is an array with one item that has metadata and one that has data
        const metadataItem = value.find((item) => item.metadata);
        const dataItem = value.find((item) => item.data);
        if (!metadataItem || !dataItem) {
            return { headers: [], rows: [] };
        }

        const metadata = metadataItem.metadata;
        const data = dataItem.data;

        // Create headers array from metadata
        const headers = metadata.map((col) => Object.keys(col)[0]);

        // Transform data array into array of objects
        const dataRows = data.map((row) => {
            return row.reduce((acc, val, index) => {
                // acc is the accumulator object
                // val is the current value in the row
                // index is the current index of val in the row to find the corresponding header name in headers
                acc[headers[index]] = val;
                return acc;
            }, {});
        });

        return { headers, rows: dataRows };
    }

    function buildTable(data) {
        const table = $("<table/>").addClass("my-table");

        // Build the header
        const thead = $("<thead/>");
        const headerRow = $("<tr/>");
        data.headers.forEach((header) => {
            headerRow.append($("<th/>").text(header));
        });
        thead.append(headerRow);
        table.append(thead);

        // Build the body
        const tbody = $("<tbody/>");
        data.rows.forEach((row) => {
            const tr = $("<tr/>");
            data.headers.forEach((header) => {
                tr.append($("<td/>").text(row[header]));
            });
            tbody.append(tr);
        });
        table.append(tbody);

        return table;
    }
</script>
;

// Change to use the entire JSON response
let tableProps = {
json: pm.response.json(),
};

pm.visualizer.set(template, tableProps);

`

Could you confirm if there have been changes in the latest Postman update that affect the pm object or the Visualizer feature? If so, could you provide guidance on how to adapt the script for compatibility with the updated version?

Thank you for your assistance!
a13b060f9a5aa32f8c1ebc531c484ebb607d31da

Steps To Reproduce

  1. Navigate to the "Scripts" Tab
  2. Paste the code in the post-response
  3. Click Visualize

Screenshots or Videos

No response

Operating System

Windows

Postman Version

11.27.3

Postman Platform

Postman App

User Account Type

Signed In User

Additional Context?

No response

@jonathanhaviv
Copy link

@aksevenli thanks for bringing this to our attention. I was able to reproduce this bug and we are currently investigating the cause. I'll let you know when we have released a fix.

I was able to get the script working on the web app. Would you be able to use this as a workaround until we have a fix?

@aksevenli
Copy link
Author

aksevenli commented Jan 10, 2025

For some reason I cannot use the web app, because our domain is not public, but currently I can still work with a older version of the desktop app.

@aksevenli
Copy link
Author

I installed Postman desktop agent to be able to use the web app, the script works, when using the web app. But it does not work on the Desktop App.

@avechuche
Copy link

Hi! I have the same problem with v11.27.4 version

@thamyrisSoares
Copy link

Facing the same issue, or even worse v11.28.0

Image

@jonathanhaviv
Copy link

@aksevenli we have released a fix for the issue you mentioned in the latest desktop release would you be able to update your app and confirm if it is working for you?

@thamyrisSoares @avechuche would y'all be able to try testing on the latest release? If you're still having issues could you share the request, script, and/or curl for the requests that aren't working for you?

@avechuche
Copy link

@jonathanhaviv Hello! Thanks for the news. The latest version (11.28.2) works perfectly

@aksevenli
Copy link
Author

@jonathanhaviv Thanks, it is working again!

@jonathanhaviv
Copy link

Great I'm going to close this, but let us know if you have any other issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants