Created
December 18, 2015 20:02
-
-
Save timelf123/a46d9f03cea65d515337 to your computer and use it in GitHub Desktop.
This file contains 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
idea: function(req, res, next, id) { | |
if (!req.challenge) return next(new Error('Failed to load challenge idea ' + id)); | |
var challenge = req.challenge; | |
req.idea = challenge.ideas.id(id); // DocumentArrays have a special id method for looking up a document by its _id. | |
// req.idea = challenge.ideas.id(id).populate('user', 'name username') this is what I want to do | |
next(); |
This file contains 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
'use strict'; | |
/** | |
* Module dependencies. | |
*/ | |
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema; | |
/** | |
* Idea Schema | |
* | |
* A contest can have multiple ideas | |
*/ | |
var IdeaSchema = new Schema({ | |
user: { | |
type: Schema.Types.ObjectId, | |
ref: 'User' | |
}, | |
challenge: { | |
type: Schema.Types.ObjectId, | |
ref: 'Challenge' | |
}, | |
created: { | |
type: Date, | |
default: Date.now | |
}, | |
title: { | |
type: String, | |
required: true, | |
trim: true | |
}, | |
description: { | |
type: String, | |
required: true, | |
trim: true | |
}, | |
permissions: { | |
type: Array | |
}, | |
updated: { | |
type: Array | |
}, | |
status: String | |
}); | |
/** | |
* Validations | |
*/ | |
IdeaSchema.path('title').validate(function(title) { | |
return !!title; | |
}, 'Title cannot be blank'); | |
IdeaSchema.path('description').validate(function(description) { | |
return !!description; | |
}, 'description cannot be blank'); | |
/** | |
* Statics | |
*/ | |
IdeaSchema.statics.load = function(id, cb) { | |
this.findOne({ | |
_id: id | |
}) | |
.populate('user', 'name username') | |
.populate('challenge', 'title') | |
.exec(cb); | |
}; | |
mongoose.model('Idea', IdeaSchema); | |
/** | |
* Challenge Schema | |
*/ | |
var ChallengeSchema = new Schema({ | |
created: { | |
type: Date, | |
default: Date.now | |
}, | |
title: { | |
type: String, | |
required: true, | |
trim: true | |
}, | |
description: { | |
type: String, | |
required: true, | |
trim: true | |
}, | |
tagName: { | |
type: String | |
}, | |
user: { | |
type: Schema.Types.ObjectId, | |
ref: 'User' | |
}, | |
ideas: [IdeaSchema], | |
submissions: { | |
type: Number, | |
default: 0 | |
}, | |
permissions: { | |
type: Array | |
}, | |
updated: { | |
type: Array | |
}, | |
startDate: { | |
type: Date, | |
default: Date.now | |
}, | |
endDate: { | |
type: Date | |
}, | |
status: String | |
}); | |
/** | |
* Validations | |
*/ | |
ChallengeSchema.path('title').validate(function(title) { | |
return !!title; | |
}, 'Title cannot be blank'); | |
ChallengeSchema.path('description').validate(function(description) { | |
return !!description; | |
}, 'description cannot be blank'); | |
/** | |
* Statics | |
*/ | |
ChallengeSchema.statics.load = function(id, cb) { | |
this.findOne({ | |
_id: id | |
}).populate('user', 'name username').exec(cb); | |
}; | |
mongoose.model('Challenge', ChallengeSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment