2015-07-03 101 views
3

您好我是Sails的新手,我试图通过禁用水线功能使用Sails连接到Mongoose。在执行我的帆代码POST操作,我我遇到以下问题风帆错误:抛出新的错误('发送后无法设置标题')

/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:108 
if (this.ended && !this.hasRejectListeners()) throw reason; 
               ^
Error: Can't set headers after they are sent. 
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11) 
at ServerResponse.res.setHeader (/usr/local/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:133:22) 
at ServerResponse.res.set.res.header (/usr/local/lib/node_modules/sails/node_modules/express/lib/response.js:577:10) 
at ServerResponse.res.send (/usr/local/lib/node_modules/sails/node_modules/express/lib/response.js:142:12) 
at ServerResponse.res.json (/usr/local/lib/node_modules/sails/node_modules/express/lib/response.js:223:15) 
at EventEmitter.<anonymous> (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/api/controllers/Status-packetController.js:74:17) 
at EventEmitter.<anonymous> (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45) 
at EventEmitter.emit (events.js:110:17) 
at Promise.safeEmit (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21) 
at Promise.fulfill (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24) 
at Promise.resolve (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/lib/promise.js:113:23) 
at model.<anonymous> (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/lib/document.js:1578:39) 
at next_ (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:89:34) 
at EventEmitter.fnWrapper (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:171:15) 
at EventEmitter.<anonymous> (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45) 
at EventEmitter.emit (events.js:110:17) 
at Promise.safeEmit (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21) 
at Promise.fulfill (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24) 
at p1.then.then.self.isNew (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/lib/model.js:254:27) 
at newTickHandler (/Users/febinp/Documents/Tecsol/Taqua/Taqua-loginpacket/node_modules/mongoose/node_modules/mpromise/lib/promise.js:229:18) 
at process._tickDomainCallback (node.js:381:11) 

我的代码如下

CreateStatus: function (req, res) { 
    var params = req.body; 
    if(params.SerialNo == null) { 
    var response = {}; 
    response.success = "FALSE"; 
    response.error = "Serial.NO.is.Null"; 
    res.json(response); 
    return(res); 
    } 
    // For Authenfication purpose Only 
    var mongoose = require('mongoose'); 
    var login = mongoose.model('Login'); 
    var SerialNo1 = req.body.SerialNo; 

    login.find({ "SerialNo": SerialNo1 }, function (err,data) { 
    if(err) { 
     res.json({ 
     success: false, 
     error: err 
     }); 
     return;  
    } else { 
     if(data.length <= 0) { 
     res.json({ 
      success: false, 
      error: "User Doesn't Exists" 
     }); 
     return;   
     } 
    } 
    }); 
    // End of Authentification 

    var msg_parts = params.msg_parts; 


    // CONVERSION ALGORITHM FROM INTEGER TO BOOLEAN 
    if(msg_parts.chargingStatus <= 0) 
    msg_parts.chargingStatus = false; 
    else 
    msg_parts.chargingStatus = true; 

    if(msg_parts.alarmInfo <= 0) 
    msg_parts.alarmInfo = false; 
    else 
    msg_parts.alarmInfo = true; 

    if(msg_parts.gpsStatus <= 0) 
    msg_parts.gpsStatus = false; 
    else 
    msg_parts.gpsStatus = true; 

    msg_parts.SerialNo = params.SerialNo; // Adding the Serial no into the message parts 
    // CONVERSION ALGORITHM FROM INTEGER TO BOOLEAN finished 

    var statuspacket = db.StatusPacket(msg_parts); 
    // For saving into the Database 
    statuspacket.save(function (err,data) { 
    if(err) { 
     res.json({ 
     success: false, 
     exception: err 
     }); 
     return; 
    } 
    res.json({ 
     success: true, 
     data:data 
    }); 
    return; 
    }); 
} 

基本上我的代码只是把数据输入到数据库中,这样做之前,执行用户的认证部分。一旦我得到DHCP并点击后我遇到了上面提到的问题。我的数据正在被储存到数据库中,但是帆正在下降。我的模式是如下

// Serial No of the device.We require an Index on this column 
SerialNo: { 
    type: String, 
    required: true 
}, 
batteryLevel: { 
    type: Number, 
    min: 0, // battery level will take values from 0 to 6(0,1,2,3,4,5,6) 
    max: 6 
}, 
signalLevel: { 
    type: Number, 
    min: 0, // Signal Level will take values from 0 to 4 (0,1,2,3,4) 
    max: 4, 
}, 
chargingStatus: { 
    type: Boolean, 
    default: false 
}, 
alarmInfo: { 
    type: Boolean, 
    default: false 
}, 
gpsStatus: { 
    type: Boolean 
}, 
createdAt: { 
    type: Date, 
    default: Date.now 
}, 
PurgeDataNumber: { 
    type:Number 
} 

我的路线

'/Status-packet/CreateStatus': 'Status-packetController.CreateStatus', 
'/Status-packet/FindStatus': 'Status-packetController.FindStatus' 

有人可以帮我这个问题,为什么我的船帆的应用程序要倒在岗位操作?

回答

7
Error: Can't set headers after they are sent. 

As @nfleury said,this er当您尝试多次向客户端发送响应(res.*)时,会抛出ror。

让我们来看看这个问题。

起初看,没有问题。每次使用res.json时,请致电return

问题一定比较复杂。也许是一个异步函数的问题?

CreateStatus: function (req, res) { 
    // No problem at the beginning 
    // ... 

    // Here you call an asynchronous function 
    // Let's name it "function 1" 
    login.find({ "SerialNo": SerialNo1 }, function (err, data) { 
    if(...) { 
     res.json(...); 
     return;  
    } 
    }); 
    // You've called function 1 but the code continues to be executed 

    // ... 

    // Another async function is called 
    // Let's name it "function 2" 
    statuspacket.save(function (err, data) { 
    res.json(...); 
    return; 
    }); 
} 

正如我们所看到的,函数1和函数2都被调用。我假设问题发生在回调被调用时。

功能2回调总是向客户端发送响应。如果一系列条件得到验证,函数1回调发送回应。得到它了!

现在。我们如何解决这个问题?

我认为你想要做的是等到第一个回调被调用后再继续。你可以这样做:

login.find({ "SerialNo": SerialNo1 }, function (err, data) { 
    // Function 1 has complete 
    if(...) { 
    res.json(...); 
    return;  
    } 

    // ... 

    statuspacket.save(function (err, data) { 
    res.json(...); 
    return; 
    }); 
}); 
+0

它的工作Yann Bertrand !!!!感谢您的帮助 – shubhamagiwal92

+0

在我的情况下,找到多余的res.send()做到了。谢谢! – eyn

-2
Error: Can't set headers after they are sent. 

通常当你有这样的错误就意味着你发送一个响应两次......

这可能是由这个原因引起的:

statuspacket.save(function (err,data) { 
    if(err) { 
    res.json({ 
     success: false, 
     exception: err 
    }); 
    return; 
    } 
    res.json({ 
    success: true, 
    data: data 
    }); 
    return; 
}); 

如果你有一个错误就会发送再次json响应...看起来像你在这里缺少'else'...

+0

你的第一句话是对的。但是由于他使用了“return”,所以没有更多的事情发生。所以你在答案的第二部分是错误的。 –

+0

对,我错过了... – nfleury