即使JSON数据在MongoDB上持久化,nodeJS方法也会报错
我是nodeJS和mongoDB的新手。 使用nodeJS作为中间件创build应用程序,使用mongoDB作为数据库,使用UI中的angularJS。
在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." }); } });
请帮忙!!!!!!
一旦在控制器中检查出它是否为student._id
因为默认情况下,节点以“ _
”作为前缀创buildid