-
-
Save jhony112/3a9de0e1ef306a94961813d6adc4cb17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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