【发布时间】:2016-07-31 00:41:04
【问题描述】:
我正在使用 Angular 表单,我想检索每个单选按钮选择,以便将这些选择保存到我的 Picks.js mongoose 模型的实例中。我知道我在 Angular 表单和 Node(Express) 服务器方面设置正确,因为 user._id 和竞赛._id 保存在 mongodb 数据库中,但 selectedTeams 数组保存为空数组(selectedTeams: [ ])。我通过谷歌研究了很长时间,真的找不到任何具体的解决方案来解决我的问题。任何建议或答案将不胜感激。
控制器
function submitPicks() {
$scope.buttonDisabled = true;
$scope.hasMadePicks = true;
contestService.participate(contest);
angular.forEach($scope.contest.matchups, function(matchup) {
$scope.selectedTeams.push(matchup.selectedTeam);
});
contestService.createEntry(contest._id, {
user: authService.currentUserId(),
selectedTeams: $scope.selectedTeams
})
}
服务
function createEntry(id, pick) {
return $http.post("/contests/" + id + "/picks", pick, {
headers: {
Authorization: "Bearer " + authService.getToken()
}
});
}
路由器
router.route("/contests/:contest/picks")
.post(auth, function(req, res, next) {
var pick = new Pick();
pick.contest = req.contest;
pick.save(function(err, pick) {
if(err) {
return next(err);
}
req.contest.picks.push(pick);
req.contest.save(function(err, contest) {
if(err) {
return next(err);
}
Pick.populate(pick, {
path: "user",
select: "username"
}).then(function(pick) {
res.json(pick);
});
})
})
});
HTML
{{contest.participants}}
{{contest.usersWhoJoined}}
<form name="contestForm" ng-submit="submitPicks()">
<table id="nbateams">
<tr>
<th>Home Team</th>
<th>Away Team</th>
<th>Selected Team</th>
<th>Winning Team</th>
</tr>
<tr ng-repeat="matchup in contest.matchups">
<td>
<input type="radio" name="matchup{{matchup.matchupId}}" ng-model="matchup.selectedTeam" ng-value="matchup.homeTeam">
<img ng-src="{{matchup.homeLogo}}">
<br>
{{matchup.homeTeam}}
</td>
<td>
<input type="radio" name="matchup{{matchup.matchupId}}" ng-model="matchup.selectedTeam" ng-value="matchup.awayTeam">
<img ng-src="{{matchup.awayLogo}}">
<br>
{{matchup.awayTeam}}</td>
<td>{{matchup.selectedTeam}}</td>
<td>{{matchup.winningTeam}}</td>
</tr>
</table>
<button type="submit" ng-show="!hasMadePicks" ng-disabled= "buttonDisabled" class="btn btn-success">Submit Picks</button>
</form>
{{selectedTeams}}
代表每个单选按钮值的正确字符串值在 {{selectedTeams}} 中正确显示,我只是无法将其添加到 Pick 模型中的 pick.selectedTeams 变量中。这是我一直引用的 Pick 模型的架构:
var mongoose = require("mongoose");
var PickSchema = new mongoose.Schema({
contest: {
type: mongoose.Schema.Types.ObjectId,
ref: "Contest"
},
selectedTeams: [String],
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
});
module.exports = mongoose.model('Pick', PickSchema);
【问题讨论】:
标签: javascript angularjs node.js mongodb mongoose