[英]Express Route stops working after a set amount of Angularjs Post
I'm having some trouble getting AngularJS, Express and MongoDB to work together. 我在让AngularJS,Express和MongoDB协同工作时遇到了一些麻烦。 I'm still new to all three of these and trying to make a simple 'to do' app and toggle a check button to mark something complete.
我对这三者仍然不熟悉,并尝试制作一个简单的“要做”应用程序并切换一个检查按钮以标记完成。 I able to execute a function on the angular side that will do a $http 'POST' to an express route that ultimately updates a MongoDB record.
我能够在有角度的一侧执行一个函数,该函数将对显式路由执行$ http'POST',最终更新MongoDB记录。 I am able to do this 6 times before it stops working.
在停止工作之前,我能够执行6次此操作。 I can see in the inspector console that the function is still being called, but somewhere between the $http 'POST' and executing the route, it doesn't register it anymore.
我可以在检查器控制台中看到该函数仍在被调用,但是在$ http'POST'和执行路由之间的某个地方,它不再注册它。 I no longer see console.log activity on my Express server from the route.
我不再从该路由在Express服务器上看到console.log活动。
Here is my toDoController.js 这是我的toDoController.js
toDoApp.controller('ToDoController', function ToDoController($scope, $http, itemsData) {
... other functions
$scope.toggleToDo = function(item) {
console.log('updateToDo()');
console.log(item);
$http({
method: 'POST',
url: '/todolist/toggle_to_do',
data: item
}).
success(function(data, status, headers, config) {
console.log('success post');
}).
error(function(data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
});
}
}
Express app.js route Express app.js路线
app.post('/todolist/toggle_to_do', todolist.toggleToDo);
Express route.js Express route.js
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID; // used to create ObjectID when looking for the record
var collection
var database
// Connect to Database ------------------------------------------------
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db){
if(err) throw err;
console.log('\033[96m + \033[39m connected to mongodb');
collection = db.collection('to_do_list');
database = db;
}
);
// update record
exports.toggleToDo = function(req, res) {
console.log("-toogle 'To Do item' completed status");
console.log(req.body.completed);
console.log(req.body._id);
collection.update({
_id: new ObjectID(req.body._id)
},
{
$set: {
completed: req.body.completed
}
},
function(err) {
if(err) console.warn("Could not write to DB");
else console.log("Item successfully updated!");
}
);
};
Any help would be appreciated! 任何帮助,将不胜感激! Thanks!
谢谢!
Looks like I didn't end my response. 看来我没有结束回应。
res.end(); 重发();
exports.toggleToDo = function(req, res) {
console.log("-toogle 'To Do item' completed status");
console.log(req.body.completed);
console.log(req.body._id);
collection.update({
_id: new ObjectID(req.body._id)
},
{
$set: {
completed: req.body.completed
}
},
function(err) {
if(err) console.warn("Could not write to DB");
else console.log("Item successfully updated!");
}
);
res.end();
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.