2015-12-14 114 views
0

我有以下3個文件。我的錯誤是「發送後無法設置標題」。我見過this,但我無法找出哪些函數或回調導致發送後寫入標頭。我花了很多時間。請幫忙。發送錯誤後無法設置標題快遞

routes.js

"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 
}; 
+0

你並不需要在控制器返回標籤,只需要使用回調函數不返回標籤!並且不要在控制器的任何地方使用返回標籤,只需使用回調函數! –

回答

0

裏面你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 
相關問題