Skip to content

Instantly share code, notes, and snippets.

@jhony112
Forked from sunnexy/question.js
Last active November 27, 2019 08:59
Show Gist options
  • Save jhony112/3a9de0e1ef306a94961813d6adc4cb17 to your computer and use it in GitHub Desktop.
Save jhony112/3a9de0e1ef306a94961813d6adc4cb17 to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose');
const questionSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
createdOn: { type: Date, default: Date.Now },
createdBy: { type: String, ref: 'User' }, // represents the user asking the question
meetup: { type: mongoose.Schema.Types.ObjectId, ref: 'Meetup', required: true }, // represents the meetup the question is for
title: { type: String },
bodyMessage: { type: String },
votes: { type: mongoose.Schema.Types.ObjectId
// upvotes: {type: Number, default: 0, ref: 'Vote'},
// downvotes: {type: Number, default: 0, ref: 'Vote'},
}
});
module.exports = mongoose.model('Question', questionSchema);
router.get('/', checkAuth, (req, res, next) => {
Question.find({})
//.populate('votes')
.select('_id meetup title body createdBy votes')
.exec()
.then(records => {
records.forEach(async function (record) {
record.votes = await getVotes(record._id)
})
res.status(200).json({
count:records.length,
questions: records
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
})
});
async function getVotes(qid) {
Votes.find({questionId:qid}).select('upvotes, downvotes').exec()
.then(votes => {
return votes;
})
.catch(err => {
return null
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment