Reputation: 33
Hi everyone this question is kind of silly but i am new to programming. So i have been developing an product management system for my university project using ejs, nodejs, express, mongoose, mongodb. so i am not able to figure out how to get the category title instead of the objectId. I tried using <td> <%= issue.product.category.title%></td>
but it gets blank.
<table class="table table-bordered">
<thead class="bg-dark text-center">
<tr class="text-white">
<th>Employee Name</th>
<th>Email</th>
<th>Employee Number</th>
<th>Contact Number</th>
<th>Product ID</th>
<th>Title</th>
<th>Manufacturer</th>
<th>Status</th>
<th>Category</th>
<th>Date/Time</th>
</tr>
</thead>
<tbody class="text-center">
<% if (issue.length> 0) { %> <% issue.forEach(issue=> { %>
<tr>
<td> <%= issue.ename %></td>
<td><%= issue.email %></td>
<td><%= issue.enumber %></td>
<td><%= issue.cnumber %></td>
<td><%= issue.product.prodid%></td>
<td><%= issue.product.title%></td>
<td><%= issue.product.manufacturer%></td>
<td><%= issue.product.status%></td>
<td> <%= issue.product.category%></td>
<td><%= issue.issueTime %> </td>
<% }) %> <% } else { %>
<p>There are no issue to display...</p>
<% } %>
</tr>
</tbody>
</table>
const issueSchema = new Schema({
ename: {
type: String,
required: true
},
email: {
type: String,
required: true
},
enumber: {
type: String,
required: true
},
cnumber: {
type: String,
required: true
},
desig: {
type: String,
required: true
},
department: {
type: String,
required: true
},
description: {
type: String,
required: true
},
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Prodcut'
},
issueTime: {
type: Date,
default: Date.now()
}
});
const Issue = mongoose.model('issue',issueSchema);
module.exports = Issue;
Here, i have embedded product collection.
const productSchema = new Schema({
prodid: {
type: String,
required: true
},
title: {
type: String,
required: true
},
manufacturer: {
type: String,
required: true
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'category'
},
status: {
type: String,
default: 'In Stock'
},
coverImage: {
type: Buffer,
required: true
},
coverImageType: {
type: String,
required: true
}
}, { timestamps: true });
const CategorySchema = new Schema({
title: {
type: String,
required: true
}
}, { timestamps: true });
const Category = mongoose.model('category',CategorySchema);
module.exports = Category;
const issue_detail = (req, res) => {
Issue.find().sort({ createdAt: -1})
.populate('product category')
.then((issue) => {
res.render('products/issue/details', {
issue: issue,
})
})
.catch((err) => {
console.log(err);
})
};
Thankyou in advance!
Upvotes: 0
Views: 117
Reputation: 128
const issue_detail = (req, res) => {
Issue.find().sort({ createdAt: -1})
.populate({path : 'product', populate : {path : 'category'}})
.then((issue) => {
res.render('products/issue/details', {
issue: issue,
})
})
.catch((err) => {
console.log(err);
})
Reference: http://mongoosejs.com/docs/populate.html#deep-populate
Upvotes: 1
Reputation: 1538
It's a little hard to say just from the code alone.
What would be helpful is to console.log what you are getting back from the DB, so in the head of ejs just pus
<% console.log(issues) %>
This will display the returned object to your view, from there you will see what is being returned, then you can reference the correct element in your object.
Upvotes: 0