0
我正在試驗MEAN協議棧,特別是MEAN.js。MEAN實體協會
雖然文檔中的所有內容都有很好的解釋,但似乎將實體(或模型)與另一個實體相關聯的簡單任務在文檔或示例中沒有解釋。
例如,很容易產生一個想法和一個民意調查crud。但是如果我必須將「民意調查」與一個「想法」聯繫起來,那麼一個多對多的關係呢?
我想我會做一些類似於此的polls.client.controller.js:
// 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;
});
};
但當角模型推到Express.js後臺,我看不出任何痕跡在關於Idea的請求中,我唯一得到的是Poll。
/**
* 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);
我相信,有件事我做錯了,但任何解釋(或鏈接)如何執行此任務,超出此特定錯誤或設置我的將不勝感激。