问题
I'm developing a polls application, in which a user can vote for an option in a given poll. each poll has 2 or more options as subdocument. each of these options have votes that are documents in another collection (for authentication and unique voting purposes).
I have the polls CRUD working (I can create, read, update and delete without a problem), but my problem begins when im trying to create a vote function, i.e update a poll documents poll_option subdocument + creating a new vote document.
poll.server.model.js
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Poll Schema
*/
var PollSchema = new Schema({
poll_id: {type:Number},
user: {
type: Schema.ObjectId,
ref: 'User'
},
poll_question: {type:String},
poll_language: [{
type:Schema.ObjectId,
ref: 'Language'
}],
poll_category: [{
type: Schema.ObjectId,
ref: 'Category'
}],
poll_description: {type:String},
poll_description_raw: {type:String},
poll_weight_additional: {type:Number},
poll_flag_active:{type:Number,default:1},
poll_flag_18plus:{type:Number,default:0},
poll_flag_expire:{type:Number,default:0},
poll_flag_deleted:{type:Number,default:0},
poll_flag_moderated:{type:Number,default:0},
poll_flag_favourised:{type:Number,default:0},
poll_date_expiration:{type:Date},
poll_date_inserted:{type:Date,default:Date.now},
poll_flag_updated:{type:Date},
show_thumbs:{type:Boolean},
comments: [{
type: Schema.ObjectId,
ref: 'Comment'
}],
poll_options: [{
option_text:{type:String},
option_thumb:{type:Number,default:0},
votes:[{
type: Schema.ObjectId,
ref: 'Vote'
}]
}]
});
mongoose.model('Poll', PollSchema);
but ill start from the front, this is the vote function in the frontside controller
// Vote
$scope.vote = function(){
$scope.votes = Votes.query();
var vote = new Votes({
_id:pollId,
option_id:optionId
});
vote.$save(function(response){
// ... //
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
here is the Votes factory:
angular.module('polls').factory('Votes', [ '$resource',
function($resource) {
return $resource('polls/:pollId/votes/:optionId', {
pollId: '@_id',
optionId: '@option_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
up to this point everything runs well, i.e when i run the $scope.vote(); function i get this response in the browser console:
POST http://localhost:3000/polls/548c6da001ec1f4ba2860c38/votes/548c6da001ec1f4ba2860c3a 404 (Not Found)
from this i gather that the call to that url is made, controller + service (angular) working.
following the meanjs article example, i understand that i need to map the optionId param to an actual option
poll.server.route.js
'use strict';
/**
* Module dependencies.
*/
var users = require('../../app/controllers/users.server.controller'),
polls = require('../../app/controllers/polls.server.controller');
module.exports = function(app) {
// Poll Routes
app.route('/polls')
.get(polls.list)
.post(polls.create);
app.route('/polls/:pollId')
.get(polls.read)
.put(polls.update)
.delete(polls.delete);
app.route('/polls/:pollId/votes/:optionId')
.put(polls.vote);
app.param('pollId', polls.pollByID);
app.param('optionId', polls.pollOptionByID);
};
but no matter what i do, i keep on getting the 404! here is the polls.pollOptionByID function in the polls.server.controller.js
exports.pollOptionByID = function(req, res, next, id) {
Poll.findOne({'poll_options._id':id}).exec(function(err,poll_option){
console.log('hi');
if (err) return next(err);
if (!poll) return next(new Error('Failed to load poll option ' + id));
req.poll_option = poll_option;
next();
});
}
but I don't even get there. I don't see that hi in the console log. and yes, of course I tried without the console.log but nothing works, I keep getting only 404. What am I doing wrong? How can I achieve my goals i.e creating a new vote document + mapping it to a poll_option subdocument in a given poll document?
回答1:
so, as wellington zhao (https://www.facebook.com/AlphanumericSoup?fref=ufi) at the meanjs fb group (https://www.facebook.com/groups/meanjs/463004417186215/?comment_id=463027443850579¬if_t=group_comment) pointed out:
It looks like you're trying to POST to a route (/polls/:pollId/votes/:optionsId) that only has PUT defined. Change it to one or the other and see if 404 persists.
so i changed the route definition to post and viola, it worked! hoped i help other noobs to avoid hours of cursing and shouting why.
来源:https://stackoverflow.com/questions/27464146/how-to-create-a-new-document-update-an-existing-document