I keep encountering a persistent TypeError
message stating that the length is undefined
in the following code snippet. This issue is quite frustrating, especially since I've utilized the same code elsewhere in my project without any problems.
users.js
router.get('/', function(req, res) {
MongoClient.connect(url, function(err, db) {
if (err) {
console.log('unable to connect');
} else {
collection = db.collection('users');
collection.find({}).toArray(function(err, result) {
if (err) {
res.send(err);
} else if (result.length) {
res.render('users', {
"users": result
});
} else {
res.send('No documents found')
};
db.close()
});
}
});
});
users.pug
The error messages pinpoint line 11 (each user, i in users
) within the following code
extends layout.pug
block content
h3 #{title}
ul
li
a(href="users/add") Add New user
ul
each user, i in users <--- Error occurs here
li
p #{users._id}
li
p #{users.name}
li
p #{users.email}
li
p #{users.username}
li
p #{users.password}
If you have any suggestions or solutions to help me troubleshoot this problem, I would greatly appreciate it. Thank you in advance :)
UPDATE:
I have resolved the issue by identifying and removing a duplicate router.get('/')
route. If you are facing a similar problem, make sure to double-check for any duplicated routes.