2015-07-03 2 views
3

Привет, я новичок в Sails, и я пытаюсь подключиться к Mongoose с помощью Sails, отключив функции ватерлинии. При выполнении моего кода в парусов для операции POST, я встречая следующую задачуSails Error: throw new Error ('Can ' t установить заголовки после их отправки. ')

/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. 

Как сказал @nfleury, этот e rror бросается, когда вы пытаетесь отправить ответ (res.*) несколько раз клиенту.

Давайте найдем проблему.

На первый взгляд, проблем нет. Вы звоните return каждый раз, когда используете res.json.

Тогда проблема должна быть более сложной. Может быть, проблема с асинхронной функцией?

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 callback всегда посылает ответ клиенту. Если последовательность условий подтверждена, функция 1 callback также отправляет ответ. Понял!

Теперь. Как мы можем это решить?

Я думаю, что вы хотите дождаться, пока первый вызов не будет вызван, прежде чем продолжить. Вы можете сделать это вот так:

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

    // ... 

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

Это сработало Янн Бертран !!!! Спасибо за вашу помощь – 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

Смежные вопросы