Seeking to access the Question model's text referenced in the Survey model, particularly the QuestionText. While I am able to retrieve the question ID successfully, obtaining the QuestionText using
SurveyList[count].Questions.QuestionText
does not work as expected.
However, this line does function correctly:
SurveyList[count].Questions._id
The complete front-end code snippet is provided below:
<!-- All Surveys -->
<% for (let count = 0; count < SurveyList.length; count++) { %>
<tr>
<!-- Display title -->
<td class="text-center text-white"><%= SurveyList[count].Title %></td>
<!-- Display type -->
<td class="text-center text-white"><%= SurveyList[count].Type %></td>
<td class="text-center text-white"><%= SurveyList[count].Questions._id %></td>
<% } %>
Details of my Question Model Schema are outlined below:
// Define the question model
let questionModel = mongoose.Schema(
{
QuestionText: String,
Options: String,
},
{
collection: "questions",
}
);
Information regarding my Survey model schema is presented here:
let surveyModel = mongoose.Schema(
{
Title: String,
Type: [String],
Questions: { type: mongoose.Schema.Types.ObjectId, ref: "questions" },
Answered: { type: Number, default: 0 }, // number of user responses
DateCreated: { type: Date, default: Date.now }, // date created
Lifetime: { type: Date, default: Date.now }, // Survey expiration date
},
{
collection: "surveys",
}
);
Controller logic for displaying live surveys is included below:
module.exports.displayLiveSurveys = (req, res, next) => {
Survey.find((err, surveyList) => {
if (err) {
return console.error(err);
} else {
res.render("content/survey/live-surveys", {
title: "Live Surveys",
page: "live-surveys",
username: req.user ? req.user.username : "",
SurveyList: surveyList,
});
}
});
};
If there is a way to reference Question.find within Survey.find and append QuestionList to res.render, that could be advantageous. Attempted implementation did not yield desired results.
Data payload for a Survey object:
{
"_id": {
"$oid": "60fd0c7ecd479a846f1f0fe5"
},
"Type": ["TF"],
"Answered": {
"$numberInt": "0"
},
"Title": "hello",
"Questions": {
"$oid": "60fd067d736566143839e3fd"
},
"DateCreated": {
"$date": {
"$numberLong": "1627195005136"
}
},
"Lifetime": {
"$date": {
"$numberLong": "1627195005136"
}
},
"__v": {
"$numberInt": "0"
}
}
Question data payload:
{
"_id": {
"$oid": "60fd0cbacd479a846f1f0fe6"
},
"QuestionText": "test",
"Options": "tester",
"__v": {
"$numberInt": "0"
}
}