Tuesday, 30 May 2017

Node: rendering mongo data to ejs template issue

I'm trying to render properly data from mongo. I can render it but not in a list, so it gets all messy in messed up array: the render function:

app.get('/about', function (req, res) {
 //CONNECTION

    MongoClient.connect(url, function (err, db) {
        assert.equal(null, err);

        db.collection('form').find().toArray(function (err, results) {

        results = JSON.stringify(results);
            res.render('about', {
                fetchMongo: results

            });
        })   

    });

});

But in the ejs template I get errors with this:

<% fetchMongo.forEach(function (field) { %>
                <li>

            <%= field.input %>

                </li>
            <% }) %>

My mongo Object looks like this:

_id" : ObjectId("592d4b7b519d6f1a5c42fee1"),
    "field" : {
        "input" : [
            {
                "message" : "expected '' to have a length above 0 but got 0",
                "showDiff" : false,
                "actual" : 0,
                "expected" : 0,
                "name" : "AssertionError"
            }
        ]
    }
}

Can someone point me the right direction? I've searched for other similar issues but I couldn't find the solution...Thanks



via FuManchu

No comments:

Post a Comment