ExpressJS在向数据库添加logging后重新查询数据库
我新来expressJS,我想知道什么是最好的方式来重新查询数据库(在我的情况下mongo)以获得所有logging后添加一个。
exports.get = function (db) { return function (req, res) { var collection = db.get('notes'); collection.find({}, {}, function (e, docs) { res.send(docs); }); }; }; exports.create = function (db) { return function (req, res) { var title = req.body.title; var note = req.body.note; var collection = db.get('notes'); // Insert/update the note collection.insert( { "title": title, "note": note }, function (err, doc) { // If it failed, return error if (err) { res.send("There was a problem adding the information to the database. Error: "+err); } else { //res.redirect('/'); //res.json(db.get('notes')); // WHAT IS THE BEST THING TO DO HERE TO GET ALL THE RECORDS INCLUDING THE ONE I'VE JUST ADDED? exports.get(db); } } ); } };
我会replace
exports.get(db);
对于
collection.find({}, {}, function (e, docs) { res.send(docs); });
原因是你正在调用这个callback,在logging插入之后
您的exports.get
函数返回一个function
,我看到一种中间件。 Repplace
exports.get(db);
通过
exports.get(db)();