目前,我正在使用ng-model将数据插入到我的MongoDB中。有没有办法使用ng-model将数据插入MongoDB中的数组? answers 是一个数组,应该包含用户输入的4个字符串。我尝试将[0],[1],[2],[3]添加到 quiz.quizData.answers ,但它没有正确输入数据作为数组。而是像这样进入它:
"answers" : [
{
"0" : "My First Answer",
"1" : "My Second Answer"
}
],
而不是它应该如何:
"answers" : [
"My First Answer",
"My Second Answer"
],
我的HTML输入表单
<input type="text" name="answers" ng-model="quiz.quizData.answers" placeholder="enter answers here" required>
<input type="text" name="answers2" ng-model="quiz.quizData.answers" placeholder="enter other answers here" required>
我的终点
// POST request for users to post a new quiz entry
apiRouter.route('/quiz')
.post(function(req, res) {
// Create quiz object and assign to 'quiz'
var quiz = new Quiz();
// Contains the quiz question
quiz.question = req.body.question;
// Contains an array with four quiz answers
quiz.answers = req.body.answers;
// Contains one string that matches the correct answer from the array above
quiz.correctAnswer = req.body.correctAnswer;
// Identifies user creating the quiz
quiz.postedBy = req.body.postedBy;
// Identifies the category of the quiz
quiz.category = req.body.category;
// then save new quiz to database
quiz.save(function(err) {
// If an error occurs, display error message in JSON format
if (err) {
return res.json({ success: false, message: 'something went way wrong....' + err });
} else {
// If no error occurs and it saves, display success
res.json({ message: 'Quiz Created!' });
}
});
});
我的MongoDB架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// post schema
var QuizSchema = new Schema({
question: { type: String, lowercase: true, required: true },
answers: { type: Array, required: true },
correctAnswer: { type: String, required: true },
category: { type: String, lowercase: true, required: true },
postedBy: { type: String, required: true }
});
module.exports = mongoose.model('Quiz', QuizSchema);
答案 0 :(得分:0)
如果你是从一个空数组开始,你可以使用 ng-repeat 迭代一个范围:
<input ng-repeat="i in [1,2,3,4]" type="text" name="answers{{i}}" ng-model="quiz.quizData.answers[i]" placeholder="enter answers here" required>
如果您的阵列已经存在,并且您想要考虑可变数组长度,则可以使用 ng-repeat 和 $ index 。请务必添加追踪$ index ,否则您会遇到重复键问题:
<input ng-repeat="answer in quiz.quizData.answers track by $index" type="text" name="answers{{$index}}" ng-model="quiz.quizData.answers[$index]" placeholder="enter answers here" required>
答案 1 :(得分:0)
<强> HTML 强>
<input type="text" name="answer1" ng-model="answersArray[0]" placeholder="answer 1">
<input type="text" name="answer2" ng-model="answersArray[1]" placeholder="answer 2">
<input type="text" name="answer3" ng-model="answersArray[2]" placeholder="answer 3">
<input type="text" name="answer4" ng-model="answersArray[3]" placeholder="answer 4">
<input type="hidden" name="answers" ng-model="quiz.quizData.answers" placeholder="enter answers here" ng-init="quiz.quizData.answers = answersArray">
<强>控制器强>
$scope.answersArray = [
answer1 = $scope.answer1,
answer2 = $scope.answer2,
answer3 = $scope.answer3,
answer4 = $scope.answer4
];