【问题标题】:How to save radio button selections (each with a string value) to an array of strings within a specific mongoose model?如何将单选按钮选择(每个都有一个字符串值)保存到特定猫鼬模型中的字符串数组中?
【发布时间】: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


    【解决方案1】:

    我想我会提供一个更新,以防将来某个时候有人处理同样的问题。 为了在 Mongodb 中保存单选按钮选择,我对比赛控制器进行了以下调整:

    contestService.createEntry(contest._id, {
      contest: $scope.contest,
      user: authService.currentUserId(),
      selectedTeams: $scope.selectedTeams
    )}.success(function(pick) {
      $scope.contest.picks.push(pick);
    });
    }
    

    在比赛路线内:

    router.route("/contests/:contest/picks")
    .post(auth, function(req, res, next) {
      var pick = new Pick(req.body);
      pick.contestPoints = 0;
    
      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);
          });
        })
      })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 2020-05-12
      • 2011-03-16
      • 2016-01-23
      相关资源
      最近更新 更多