Why is the 'TypeError: Cannot convert undefined or null to object' happening, ONLY on rendering the view (EJS)

Viewed 28

I have an EJS view being rendered with data from a database.

<div class="bullet-wrapper">
    <ul role="list">
        <% data.features.forEach(element=> { %> <li>
                <%= element %>
            </li>
            <% }) %>
    </ul>
</div>

When this loads, the page loads but the express server crashes with the typeError.

I have used res.send(data) at all stages in my code, and it send the data fine. I have determined the type using typeof at all stages to try and debug and its returning type object at all points.

The /routes.js file determines what happens to the data after being fetched from the db using an async function.

Its been working fine untill today, (its still in development) and I cannot work out why this is happening now.

I have tried a test view:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>test-view</title>
    </head>
    <body>
        <h1>test view</h1>
        <div class="test-section">
            <h4>data is...</h4>
            <p>
                <br>
            </p>
        </div>
        <div>
            <ul>
                <% data.features.forEach(function(item){ %>
                    <li>
                        <%= item %>
                    </li>
                    <% }); %>
            </ul>
        </div>
    </body>
</html>

and the server does not crash.

The error in specifics, including call stack is :

H:\work\Paragon\paragon\server\routes.js:71
        features = Object.values(camFeatures);
                          ^

TypeError: Cannot convert undefined or null to object
    at Function.values (<anonymous>)
    at H:\work\Paragon\paragon\server\routes.js:71:20
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

The function in question is

    var features = [];
    features = Object.values(camFeatures);

And the camFeatures is created with

var camFeatures = {};
    camFeatures = await getFeatures(req);

camFeatures = await getFeatures(req); is handled in the db.js file

I have used this question and answer to try and debug, which is why I sent the data using res.send() at every stage to determine, and I still can't work it out.

I can provide any more amount of code as needed.

0 Answers
Related