Node.js Can';在express中发送头后设置头时出错
我有以下3个文件。我的错误是“发送邮件后无法设置邮件头”。我已经看到了,但我无法确定是哪个函数或回调导致在发送头后写入头。我在这上面花了很多时间。请帮忙 routes.jsNode.js Can';在express中发送头后设置头时出错,node.js,express,Node.js,Express,我有以下3个文件。我的错误是“发送邮件后无法设置邮件头”。我已经看到了,但我无法确定是哪个函数或回调导致在发送头后写入头。我在这上面花了很多时间。请帮忙 routes.js "use strict"; var controller = require("./controller"); module.exports = function(app){ app.post('/master', function(req, res) { controller.process(req, fu
"use strict";
var controller = require("./controller");
module.exports = function(app){
app.post('/master', function(req, res) {
controller.process(req, function(err, data) {
if( err ) {
return res.sendStatus(400);
} else {
res.sendStatus(200);
}
});// cb ends here
});// process ends here
}// route ends here
controller.js
module.exports.process = function(req, cb){
var jsonBody = req.body;
var type = jsonBody["strType"];
var id = jsonBody["id"];
var moment = require('moment');
if ( !type || !id ) {
return (cb(true, false));
}
if( type === "company" ) {
Company_strCode = jsonBody["Company_strCode"];
jsonBody.lastUpdate = moment().format();
delete jsonBody["strType"];
delete jsonBody["id"];
dbOps.companyUpsert(type, Company_strCode, jsonBody, cb);
dbOps.normalUpsert(type, id, jsonBody, cb);
return;
}
jsonBody.lastUpdate = moment().format();
delete jsonBody["strType"];
delete jsonBody["id"];
dbOps.normalUpsert( type, id, jsonBody, cb );
} //fnProcess ends
dbOps.js
"use strict";
var keyDef = require('../config/config.json')
var MongoClient = require('mongodb').MongoClient;
var host = keyDef.mongodb.host;
var port = keyDef.mongodb.port;
var db = keyDef.mongodb.db;
var username = keyDef.mongodb.username;
var password = keyDef.mongodb.password;
var url = "mongodb://"+host+":"+port+"/"+db;
module.exports.normalUpsert = function(type, id, jsonBody, cb){
MongoClient.connect(url, function(err, db){
if(err){
console.log("Mongodb connection error..");
return cb(true, false);
}
db.collection(type).update({"_id":id},{$set:(jsonBody)}, {upsert:true}, function(err){
if(err){
return cb(true, false);
}
cb(false, true);
});//func callback
});// mongoClient callback
};
module.exports.companyUpsert = function(type, Company_strCode, jsonBody, cb){
MongoClient.connect(url, function(err, db) {
if(err){
console.log("Mongodb connection error..");
return cb(true, false);
}
db.collection("Cinema").update({"Cinema_strCompanyCode":Company_strCode},{$set:{"tblCompany":jsonBody}}, {upsert:true}, function(err){
if(err){
return cb(true, false);
}
cb(false, true);
});//func callback
});// mongoClient callback
};
在您的if(type==“company”)
条件中,您调用cb 2次,导致res.sendStatus一口井调用2次。尝试这样的解决方法,如果它适合你
module.exports.process = function(req, cb){
var jsonBody = req.body;
var type = jsonBody["strType"];
var id = jsonBody["id"];
var moment = require('moment');
if ( !type || !id ) {
return (cb(true, false));
}
if( type === "company" ) {
Company_strCode = jsonBody["Company_strCode"];
jsonBody.lastUpdate = moment().format();
delete jsonBody["strType"];
delete jsonBody["id"];
dbOps.companyUpsert(type, Company_strCode, jsonBody, function (err, data){
if (err)
return (cb(true, false));
else
dbOps.normalUpsert(type, id, jsonBody, cb);
});
return;
}
jsonBody.lastUpdate = moment().format();
delete jsonBody["strType"];
delete jsonBody["id"];
dbOps.normalUpsert( type, id, jsonBody, cb );
} //fnProcess ends
或者我更喜欢这样的东西:
module.exports.process = function(req, cb){
var jsonBody = req.body;
var type = jsonBody["strType"];
var id = jsonBody["id"];
var moment = require('moment');
if ( !type || !id ) {
return (cb(true, false));
}
jsonBody.lastUpdate = moment().format();
delete jsonBody["strType"];
delete jsonBody["id"];
if( type === "company" ) {
Company_strCode = jsonBody["Company_strCode"];
dbOps.companyUpsert(type, Company_strCode, jsonBody, function (err, data){
if (err)
return (cb(true, false));
});
}
dbOps.normalUpsert( type, id, jsonBody, cb );
} //fnProcess ends
您不需要在控制器中返回标记,只需使用不带返回标记的回调函数即可!不要在控制器的任何地方使用返回标记,只要使用回调函数!!