I have a project mongoose schema that contains the title and description of each project, and a ticket mongoose schema that also contains the title of the project the ticket is relevant to as well as the name of the user that the ticket is assigned to. Each individual project will be displayed on a post.ejs page, and I want to display the names of users who have been assigned tickets relating to that particular project.
Here is the get request so for the post.ejs page:
app.get("/posts/:postId", isLoggedIn, function(req, res) {
const requestedPostId = req.params.postId;
ProjectDetails.findOne({_id: requestedPostId}, function(err, post) {
const tickets = TicketDetails.find({project: post.title});
console.log(tickets);
res.render("post", {title: post.title, content: post.content});
});
});
I’m first querying the project database to find the project, then I’m querying the ticket database for all tickets related to the project, but when i console.log tickets I get an output like this:
2
Answers
You are receiving cursor
You need to convert it to array
or
or
As you mentioned in your query and console of tickets, it’s returning promise, and the default promise is pending. You need to resolve that promise. Use
async-await
orthen-catch
to resolve that promise. So your project query will be like this:with
async-await
:with
then-catch
: