你能检查一下这是否有帮助 SubjectBooks 是猫鼬模式。通过 subjectName 将新书追加到主题中的查询。如果您想查找具有任何其他参数(如 _id)的对象,您可以更改更新方法的第一个参数。
节点代码放在这里
已编辑
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/*....mongoose connection creation code goes here...*/
var bookSchema=new Schema({subject_name:{type:String},books[{book_name:String, book_author:String}]
});
var SubjectBooks = mongoose.model('<collection_name>', bookSchema);
app.post('<URL mapping goes here>',function(req,res){
var subject = req.body; //You have to use body-parser
var subjectName = subject.subject_name;
var subjectModel = new SubjectBooks(subject);
subjectModel.save(function (err,mongRes) {
if(err){
res.send(err);
}
res.json(mongRes);
});
});
我创建的 Angular 代码
app.controller("mainController",["$scope",'mainService',function($scope,mainService){
$scope.subjectBookCatalogue = {
subject_name: "",
books:[{
book_name:"",
book_author:""
}]
};
$scope.saveSubjects = function (data) {
mainService.saveSubjects(data).then(function(response){
console.log(response.data);
},function(response){
});
};
$scope.subjects = [];
$scope.fetchSubjects = function () {
mainService.getAllSubjects().then(function(response){
console.log(response.data);
$scope.subjects = response.data;
},function(response){
});
};
$scope.fetchSubjects();
$scope.submitForm = function(){
console.log($scope.subjectBookCatalogue);
$scope.saveSubjects($scope.subjectBookCatalogue);
}
}]);
app.factory("mainService",["$http",function($http){
return {
saveSubjects : function(data){
return $http({
method:"POST",
url: "http://localhost:3000/v1/subject/"+data.subject_name,
data:data,
headers: {
'Content-Type': 'application/json'
}
});
},
getAllSubjects : function(){
return $http({
method:"GET",
url:"http://localhost:3000/v1/subjects"
});
}
}
}]);
这应该有助于将书籍推送到主题书籍数组中。
你可以参考这个问题