我尝试使用nodejs创建一个新的mongodb文档,这里是我的架构
var resumeSchema = mongoose.Schema(
{
user:{type: mongoose.Schema.Types.ObjectId, ref: 'User'},
education:[
{
name:{type: String, required: true},
qualification:{type: String, required: true},
startEnd:{type: String, required: true},
note:{type: String, required: true}
}
],
experience:[
{
employer:{type: String, required: true},
jobTitle:{type: String, required: true},
startEnd:{type: String, required: true},
note:{type: String, required: true}
}
]
}
)
var Resume = mongoose.model('Resume', resumeSchema);
这是我的nodejs route
router.post('/add-resume', function(req, res, next) {
var newResume = new Resume();
newResume.user = req.user._id;
newResume.education= req.body.educations;
newResume.experience = req.body.experiences;
newResume.save(function(err, resume){
if (err) {
return next(err);
}else{
console.log(resume);
}
});
});
});
req.body.educations和req.body.experiences是json数组。
我需要有关如何解决此问题的帮助
我在客户端使用角度js,发送到服务器的数据是formData。这是我的客户代码
angular
.module('app')
.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
angular
.module('app')
.controller('addresumeCtrl', addresumeCtrl);
function addresumeCtrl ($scope,ngDialog,$http) {
$scope.resume = {};
$scope.educations = [];
$scope.experiences = [];
$scope.resume.educations = $scope.educations;
$scope.resume.experiences = $scope.experiences;
$scope.addNewEducation = function() {
$scope.educations.push({});
};
$scope.addNewExperience = function() {
$scope.experiences.push({});
};
$scope.removeEducation = function(index) {
$scope.educations.splice(index, 1);
};
$scope.removeExperience = function(index) {
$scope.experiences.splice(index, 1);
};
$scope.create = function(){
var uploadUrl = "/user/add-resume";
var fd = new FormData();
for(var key in $scope.resume)
fd.append(key, $scope.resume[key]);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then(function (response) {
}, function (error) {
});
};
}
这是html
<form>
<!-- Education -->
<div class="form with-line">
<h5>Education <span>(optional)</span></h5>
<div class="form-inside">
<!-- Add Education -->
<div data-ng-repeat="education in educations" class="form boxed education-box">
<a href="" ng-click="removeEducation(educations.indexOf(education))" class="close-form button"><i class="fa fa-close"></i></a>
<input class="search-field" type="text" placeholder="School Name" ng-model="education.name"/>
<input class="search-field" type="text" placeholder="Qualification(s)" ng-model="education.qualification"/>
<input class="search-field" type="text" placeholder="Start - end date eg 2000 - 2010" ng-model="education.startEnd"/>
<textarea name="desc" id="desc" cols="30" rows="10" placeholder="Notes (optional)" ng-model="education.note" ></textarea>
</div>
<a href="" class="button gray" ng-click="addNewEducation()"><i class="fa fa-plus-circle"></i> Add Education</a>
</div>
</div>
<!-- Experience -->
<div class="form with-line">
<h5>Experience <span>(optional)</span></h5>
<div class="form-inside">
<!-- Add Experience -->
<div data-ng-repeat="experience in experiences" class="form boxed experience-box">
<a href="" ng-click="removeExperience(experiences.indexOf(experience))" class="close-form button"><i class="fa fa-close"></i></a>
<input class="search-field" type="text" placeholder="Employer" ng-model="experience.employer"/>
<input class="search-field" type="text" placeholder="Job Title" ng-model="experience.jobTitle"/>
<input class="search-field" type="text" placeholder="Start / end date eg 2000 - 2010" ng-model="experience.startEnd"/>
<textarea name="desc1" id="desc1" cols="30" rows="10" placeholder="Notes (optional)" ng-model="experience.note"></textarea>
</div>
<a href="#" class="button gray" ng-click="addNewExperience()"><i class="fa fa-plus-circle"></i> Add Experience</a>
</div>
</div>
<div class="divider margin-top-0 padding-reset"></div>
<a href="" ng-click="create()" class="button big margin-top-5">Preview <i class="fa fa-arrow-circle-right"></i></a>
</form>