I am getting error:
events.js:160
throw er; // Unhandled 'error' event
^
Error: Callback was already called.
at D:\oms-backend\node_modules\async\dist\async.js:840:32
at D:\oms-backend\services\getOrdersNoLpNumber.js:258:81
at D:\oms-backend\services\getOrdersNoLpNumber.js:531:41
at D:\oms-backend\services\getOrdersNoLpNumber.js:1030:61
at D:\oms-backend\models\exported.js:83:17
at D:\oms-backend\node_modules\mongoose\lib\model.js:3411:16
at D:\oms-backend\node_modules\mongoose\lib\services\model\applyHooks.js:144:20
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
My code which is a function and called within a asyn.eachSeries:
/**
* Save XML file after creating directory according to date
* i.e yyyy/mm/dd/file.xml, save xml at home/merchantnameftp/Orders/filename
* and save xml data and other details
* to exported table.
* @param tid
* @param lpnumber
* @param xmlContent
* @param callback
*/
var saveXmlFile = function(tid, lpnumber, xmlContent, callback)
{
try{
configuration.get(function(err, configurationsArr){
if (err)
{
console.error("[Generate XML-Save XML] Error getting storename from configurations:"+ err);
callback(err);
}
else {
var storename = "TMALL";
var base_xml_path = getConfigValueByKey(configurationsArr, 'xmlPath');
var home_base_xml_path = getConfigValueByKey(configurationsArr, 'homeXmlPath'); // home ftp path for XML.
//var serverName = getConfigValueByKey(configurationsArr, 'serverName');
var serverName = 'motherswork';
logGetOrders('home_base_xml_path');
logGetOrders(home_base_xml_path);
var name = base_xml_path + year + "/" + month + "/" + day + "/";
mkdirp(name, function (err_mkdirpXml, success_mkdirpXml)
{
if (err_mkdirpXml)
{
logGetOrders('[generateXml]: Error creating XML directory.');
//LAST STEP of saving xml to Database Exported table.
exported_model.insertExportedData(tid, lpnumber, xmlContent, xml_name, storename, null, function (err_saving_exported_data, response_exported_data) {
if (err_saving_exported_data)
{
callback(err_saving_exported_data);
}
else
{
callback(null, response_exported_data)
}
})
}
else {
var xml_name = 'Tmall_Orders_' + year + '-' + month + '-' + day + '_' + tid + '_' + shipment_method + '_' + lpnumber + '.xml';
fs.writeFile(name + xml_name, xmlContent, function (err_writing_xml_file, success_writing_xml_file) {
if (err_writing_xml_file) {
logGetOrders("[generateXml]: Error writing XML File:" + err_writing_xml_file + " for order tid:" + tid);
//LAST STEP of saving xml to Database Exported table.
exported_model.insertExportedData(tid, lpnumber, xmlContent, xml_name, storename, null, function (err_saving_exported_data, response_exported_data) {
if (err_saving_exported_data) {
callback(err_saving_exported_data);
}
else {
callback(null, response_exported_data)
}
});
}
else {
logGetOrders("[generateXml]: XML file written successfully in merchant folder for order tid:" + tid);
logGetOrders("[generateXml]: home ftp path is :" + home_base_xml_path + xml_name);
//Write XML at home ftp directory.
if(serverName == "motherswork" || serverName == "Bellroy")
{
mkdirp(home_base_xml_path, function (err_mkdirpHomeXml, success_mkdirpHomeXml)
{
if (err_mkdirpHomeXml)
{
logGetOrders("[generateXml]: Error creating home ftp directory.");
//LAST STEP of saving xml to Database Exported table.
exported_model.insertExportedData(tid, lpnumber, xmlContent, xml_name, storename, null, function (err_saving_exported_data, response_exported_data) {
if (err_saving_exported_data)
{
callback(err_saving_exported_data);
}
else
{
callback(null, response_exported_data);
}
});
}
else {
fs.writeFile(home_base_xml_path + xml_name, xmlContent, function (errWritingXmlHome, successWritingXmlHome) {
if (errWritingXmlHome)
{
logGetOrders("[generateXml]: XML not created at home ftp for order tid:" + tid);
//LAST STEP of saving xml to Database Exported table.
exported_model.insertExportedData(tid, lpnumber, xmlContent, xml_name, storename, null, function (err_saving_exported_data, response_exported_data) {
if (err_saving_exported_data)
{
callback(err_saving_exported_data);
}
else
{
callback(null, response_exported_data)
}
});
}
else
{
logGetOrders("[generateXml]: XML created at home ftp successfully for order tid:" + tid);
exported_model.insertExportedData(tid, lpnumber, xmlContent, xml_name, storename, null, function (err_saving_exported_data, response_exported_data) {
if (err_saving_exported_data)
{
callback(err_saving_exported_data);
}
else
{
callback(null, response_exported_data)
}
});
}
});
}
});
}
else
{
logGetOrders("[GenerateXML] server is not bellroy or motherswork so xml not generated for home ftp. Server name"+serverName);
exported_model.insertExportedData(tid, lpnumber, xmlContent, xml_name, storename, null, function (err_saving_exported_data, response_exported_data) {
if (err_saving_exported_data)
{
callback(err_saving_exported_data);
}
else
{
callback(null, response_exported_data)
}
});
}
}
});//
}
});
}
});
}
catch(Exception)
{
callback(Exception);
}
};
I have written this kind of code several times now and it does not cause error every time. I don't know what is wrong with this piece of code.
via Simer
No comments:
Post a Comment