所以基本上我试图将我的删除API连接到我的routes.js。我有我的插入,更新,查询以及登录API,这一切都运行良好,除非我把我的删除API它出现此错误。
错误:Route.post()需要回调函数但得到[对象未定义] 在Route。(匿名函数)[作为帖子](C:\ Users \ tester01_2 \ myproject \ node_modules \ express \ lib \ router \ route.js:202: 15)
Routes.js
var express = require('express');
var dbInsert = require('./dbInsert');
var dbQuery = require('./dbQuery');
var dbUpdate = require('./dbUpdate');
var router = express.Router();
var account = require('./account');
var dbRemove = require('./dbRemove');
//var dbImport = require('./dbImport');
router.route('/insertRecord').post(dbInsert.postCollection);
//router.route('/importrecords').post(dbImport.postCollection);
router.route('/updateRecord').post(dbUpdate.postCollection);
router.route('/queryRecord').post(dbQuery.postCollection);
router.route('/login').post(account.postCollection);
router.route('/deleteRecord').post(dbRemove.postCollection);
module.exports = router;
dbRemove.js
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
var deleteRecord = function(db, req, callback){
db.collection('documents').updateMany({ "SAM ID" : req.body.samId}, {
$set: {
"Active Status" : "0"
}
}, function(err,results){
if(err) return callback(err);
console.log('Done');
console.log(results);
var cursor = db.collection('documents').find({
"SAM ID" : req.body.samId
}).toArray(function(err,doc){
if(err)
return callback(err);
else {
console.log('Succesfully deleted');
console.log(doc);
return callback(null, JSON.stringify(doc));
}
});
}
);
};
module.exports = {
postcollection : function(req, res){
var samId = req.body.samId;
MongoClient.connect(url, function(err, db) {
if(err){
res.send(err);
res.end();
}
deleteRecord(db, req, function(err, doc){
if(err){
res.send(err);
} else{
setTimeout(function(){
res.send(doc);
res.end();
}, 2000);
}
db.close();
});
});
}
}
我似乎无法在回调函数中出现问题,因为它返回一个未定义的对象,因为我使用的格式与我的其他apis相同。感谢任何帮助,谢谢!
答案 0 :(得分:1)
尝试在dbRemove.js的module.exports中大写postCollection