我正在开发一个应用程序投票,其中用户可以投票选出在给定的民意调查的选项。每个轮询有2个或更多个选项的子文档。每个选项具有在另一个集合文件(用于认证和独特的表决而言)票。
我有投票CRUD工作(我可以创建,读取,更新,没有问题删除),但是当我尝试创建一个投票功能,即更新调查文件poll_option子文档+创建一个新的投票文件我的问题开始。
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);
但是从前面开始生病,这是正面控制器的投票功能
// 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;
});
};
这里是投票工厂:
angular.module('polls').factory('Votes', [ '$resource',
function($resource) {
return $resource('polls/:pollId/votes/:optionId', {
pollId: '@_id',
optionId: '@option_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
到现在为止一切运行良好,即当我运行$ scope.vote();功能我得到在浏览器控制台这样的响应:
POST http://localhost:3000/polls/548c6da001ec1f4ba2860c38/votes/548c6da001ec1f4ba2860c3a 404 (Not Found)
从这个我收集该呼叫到该URL制成,控制器+服务(有角度的)工作。
继meanjs文章例子,我知道我需要在optionId PARAM映射到一个实际的选择
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);
};
但无论我做什么,我一直得到404!这里是在polls.server.controller.js的polls.pollOptionByID功能
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();
});
}
但我甚至不到达那里。我没有看到在控制台日志喜。是的,我当然试过没有的console.log但没有什么工作,我不断收到404只什么我做错了什么?我怎样才能实现自己的目标即创建一个新的投票文档+一个给定的调查文件中它映射到poll_option子文档?
所以,如在该meanjs FB组(https://www.facebook.com/AlphanumericSoup?fref=ufi)惠灵顿赵(https://www.facebook.com/groups/meanjs/463004417186215/?comment_id=463027443850579¬if_t=group_comment)指出:
它看起来像你想发布到路线(/调查/:optionsId:pollId /票/)只有已经把定义。将其更改为一个或另一个,看看404仍然存在。
所以我改变路线定义发布和中提琴,它的工作!希望我帮助其他菜鸟避免骂,喊为什么小时。