1
我是nodeJS和mongoDB的新手。使用nodeJS作爲中間件創建應用程序,將mongoDB作爲數據庫,並使用UI中的angularJS。即使JSON數據在MongoDB上持久保存後,nodeJS方法也會報錯
在nodeJS中具有控制器和模型的業務邏輯以及持久化數據。目前,即使在對象插入數據庫(來自模型)之後,控制器也會遇到錯誤。
控制器代碼如下:
router.post("/add", function(req,res){
var student = req.body;
Students.add(student, function(err) {
if (err) {
throw err;
}
var respOut = JSON.stringify({id:student.id});
console.log("respOut");
res.send(respOut);
});
});
型號代碼:
exports.add = function(student, cb) {
var collection = db.get().collection('students');
collection.insert(student, function(err) {
if (err) {
throw err;
}
console.log("Record added");
});
}
在UI側使用下面angularJS代碼的數據提交到的NodeJS控制器:
mainApp.controller("addStudentController", function($scope,$http) {
var resData = {};
$scope.output = "";
var url = "/students/add";
$scope.addStudent = function(){
$http.post(url, $scope.student)
.success(function(data, status, headers, config) {
resData = angular.fromJson(data);
$scope.output = "Student entered successfully with unique Id "+resData.id;
}).error(function(data, status, headers, config) {
resStatus = status;
$scope.output = "Something went wrong. Please try again later."
});
}
});
請幫忙!!!!!!