Created
November 11, 2012 16:18
-
-
Save ograycode/4055392 to your computer and use it in GitHub Desktop.
Potential Mongoose Bug
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
var mongoose = require('mongoose'); | |
var EventUser = new mongoose.Schema({ | |
user_name: String, | |
attending: Boolean | |
}); | |
var Comments = new mongoose.Schema({ | |
date: Date, | |
text: String, | |
user_name: String | |
}); | |
var Votes = new mongoose.Schema({ | |
date: Date, | |
user_name: String, | |
is_up: Boolean | |
}); | |
var EventSchema = new mongoose.Schema({ | |
to_date: Date, | |
from_date: Date, | |
location: String, | |
name: String, | |
suggested_by: String, | |
description: String, | |
users: [EventUser], | |
comments: [Comments], | |
suggestions: [EventSchema], | |
votes: [Votes] | |
}); | |
var Event = mongoose.model('Event', EventSchema); | |
module.exports = { | |
getModel : function () { | |
return Event | |
}, | |
getNewModel : function () { | |
return new Event(); | |
} | |
}; |
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
var events = require('../models/event-mongoose.js'); | |
//Save top-level vote, method does save correctly on model.save(); | |
exports.event_vote = function (req, res) { | |
return events.getModel().findById(req.body.id, function (err, doc) { | |
if (err) { | |
res.json(500, {'error': err}); | |
} else { | |
var vote = {}; | |
vote['user_name'] = req.user.provider_displayName; | |
vote['date'] = new Date(); | |
if (req.body.isUpVote === "true") { | |
vote['is_up'] = true; | |
} else { | |
vote['is_up'] = false; | |
} | |
console.log('-------------Is Up: ' + vote['is_up']); | |
var notFound = true; | |
for (var i = 0; i < doc.votes.length; i++){ | |
if (doc.votes[i].user_name === vote['user_name']) { | |
doc.votes[i].is_up = vote['is_up']; | |
doc.votes[i].date = vote['date']; | |
notFound = false; | |
} | |
} | |
if(notFound){ | |
doc.votes.push(vote); | |
} | |
doc.save(function (err){ | |
if(err){ | |
res.json(500, {'error': err}); | |
} else { | |
res.redirect('/home'); | |
} | |
}); | |
} | |
}); | |
}; | |
//Save embedded vote, method doesn't save correctly on model.save() call | |
exports.event_suggestion_vote = function (req, res) { | |
return events.getModel().findById(req.body.eventId, function (err, doc) { | |
if (err){ | |
res.json(500, {'error': err}); | |
} else { | |
for (var i = 0; i < doc.suggestions.length; i++) { | |
console.log('----------sug id ' + doc.suggestions[i]._id); | |
console.log('----------pass in sug id ' + req.body.suggestionId); | |
console.log('----------matches ' + (doc.suggestions[i]._id == req.body.suggestionId)); | |
if (doc.suggestions[i]._id == req.body.suggestionId){ | |
//We found the correct suggestion that received the vote | |
var vote = {}; | |
vote['user_name'] = req.user.provider_displayName; | |
vote['date'] = new Date(); | |
if (req.body.isUpVote === "true") { | |
vote['is_up'] = true; | |
} else { | |
vote['is_up'] = false; | |
} | |
console.log('------------VOTE ' + vote); | |
var notFound = true; | |
for (var j = 0; j < doc.suggestions[i].votes.length; j++){ | |
if (doc.suggestions[i].votes[j].user_name === vote['user_name']) { | |
//The user has already voted, so just update there vote | |
doc.suggestions[i].votes[j].is_up = vote['is_up']; | |
doc.suggestions[i].votes[j].date = vote['date']; | |
notFound = false; | |
} | |
} | |
console.log('----------- not found ' + notFound); | |
if(notFound){ | |
console.log('----------- votes length ' + doc.suggestions[i].votes.length); | |
doc.suggestions[i].votes.push(vote); | |
console.log('----------- votes length ' + doc.suggestions[i].votes.length); | |
} | |
} | |
} | |
console.log('---------'); | |
console.log(JSON.stringify(doc)); | |
console.log('---------'); | |
doc.save(function (err){ | |
if(err){ | |
res.json(500, {'error' : err}); | |
} else { | |
res.redirect('/home'); | |
} | |
}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment