Mongoose / NodeJS - String from db is retrieved but considered as not defined Mongoose / NodeJS - String from db is retrieved but considered as not defined mongoose mongoose

Mongoose / NodeJS - String from db is retrieved but considered as not defined


Take a look at this implementation, this is how i would query schema, in firs example we reuse req.user (good), in second we make 2 database calls (bad). In your example you make 1 database call but not populating Logo field of table schema (bad).

app.get('/dashboard/:uuid', function(req, res){    // first example    // no need to query users, you already have tables field    if (!req.user) // what is userId, why you check it        // add `err` checks        return res.redirect('/');    TableSchema        .find({ _id: { $in: req.user.tables } })        .populate('logos', 'url'); // Logo schema fields        .exec(function(err, result_tables){           res.render('./pages/dashboard.ejs', {username: req.user.username, tables: result_tables});    });    // or second example    // if you still for some reason cannot use req.user.tables field    // but strongly recommend to use first one    User.findById(req.user._id, 'tables')         .exec(function (err, user_tables){            // add `err` checks            TableSchema.populate(user_tables, { path: 'logos', model: 'Logo' }, function (err, result_tables){                // add `err` checks                res.render('./pages/dashboard.ejs', {username: req.user.username, tables: result_tables});            });    });});

As per your comment

in chrome browser : " Uncaught ReferenceError: stringName is not defined " (stringName = what's in tables[0].name)

Try to use forEach operator

<script>     $(document).ready(function (){         <% tables.forEach(function(table){ %>             var newTab = "<a ommited><%= table.name %></a>"; //notice: no `"`             $(newTab).appendTo('.jumbotron');         <% }) %>    });</script>