Angularjs 平均实体协会
我正在试验MEAN堆栈,特别是MEAN.js 虽然文档中对所有内容都做了很好的解释,但文档或示例中似乎没有解释将一个实体或模型与另一个实体或模型关联的简单任务 例如,很容易为想法生成crud,为民意测验生成crud。但是,如果我必须将民意调查与一个想法联系起来,以一对多的关系,该怎么办 我假设我会在polls.client.controller.js中执行类似的操作:Angularjs 平均实体协会,angularjs,mongodb,express,mongoose,mean,Angularjs,Mongodb,Express,Mongoose,Mean,我正在试验MEAN堆栈,特别是MEAN.js 虽然文档中对所有内容都做了很好的解释,但文档或示例中似乎没有解释将一个实体或模型与另一个实体或模型关联的简单任务 例如,很容易为想法生成crud,为民意测验生成crud。但是,如果我必须将民意调查与一个想法联系起来,以一对多的关系,该怎么办 我假设我会在polls.client.controller.js中执行类似的操作: // Create new Poll $scope.create = function() { // C
// Create new Poll
$scope.create = function() {
// Create new Poll object
var poll = new Polls ({
ideaId: this.idea.ideaId,//here I associate a poll with an Idea
vote1: this.vote1,
vote2: this.vote2,
vote3: this.vote3,
vote4: this.vote4,
vote5: this.vote5
});
// Redirect after save
poll.$save(function(response) {
$location.path('polls/' + response._id);
// Clear form fields
$scope.name = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
但是当angular模型被推到Express.js后端时,我在请求中看不到任何关于这个想法的痕迹,我得到的唯一东西就是投票
/**
* Create a Poll
*/
exports.create = function(req, res) {
var poll = new Poll(req.body);
poll.user = req.user;
//poll.ideaId = req.ideaId;//undefined
poll.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(poll);
}
});
};
这是我的猫鼬模型:
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Poll Schema
*/
var PollSchema = new Schema({
vote1: {
type: Number
},
vote2: {
type: Number
},
vote3: {
type: Number
},
vote4: {
type: Number
},
vote5: {
type: Number
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
idea: {
type: Schema.ObjectId,
ref: 'Idea'
}
});
mongoose.model('Poll', PollSchema);
我确信我做错了一些事情,但如果您对如何执行此任务的解释或链接超出了我的特定错误或设置,我将不胜感激。我发现的解决方案我不确定它是否是正确的解决方案或解决方法,是用相应的id填充投票的.idea字段。\u id:
var poll = new Polls ({
idea: this.idea._id,
vote1: 5,
vote2: 3,
vote3: 3,
vote4: 1,
vote5: 2
});
此时,当我开始表达时,poll.idea具有正确的关联