Skip to content

Instantly share code, notes, and snippets.

@sunnexy
Created November 24, 2019 12:47
Show Gist options
  • Save sunnexy/0d19a93cc18b68349e0cf5dab31cf874 to your computer and use it in GitHub Desktop.
Save sunnexy/0d19a93cc18b68349e0cf5dab31cf874 to your computer and use it in GitHub Desktop.
//question model
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 => {
res.status(200).json({
count:records.length,
questions: records.map(record => {
return {
_id: record._id,
meetup: record.meetup,
title: record.title,
body: record.bodyMessage,
createdBy: record.createdBy,
votes: {
upvotes: record.votes.upvotes,
downvotes: record.votes.downvotes
}
}
})
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment