2016-01-28 31 views
0

我有一個nodejs程序只是簡單地將一個字段從一個集合複製到另一個集合。我寫了兩個。一個拷貝字段命名(字符串),另一個拷貝ids(字符串數組)。該集合並不大,大約只有900個表單要迭代。我可以看到它運行和保存的一些形式,但我不明白爲什麼會發生錯誤的程序繼續運行:nodejs使用貓鼬錯誤:致命錯誤:CALL_AND_RETRY_LAST分配失敗 - 進程內存不足

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory 

下面是程序:

var mongoose = require('mongoose'), 
    config = require('../modules/system/node-js/parseConfig'), 
    schemas = require('../modules/system/node-js/schemas.js'), 
    cde_schemas = require('../modules/cde/node-js/schemas'), 
    form_schemas = require('../modules/form/node-js/schemas'), 
    mongo_cde = require('../modules/cde/node-js/mongo-cde'), 
    async = require('async'); 

var mongoUrl = config.mongoUri; 
var conn = mongoose.createConnection(mongoUrl); 
var DataElement = conn.model('DataElement', cde_schemas.dataElementSchema); 
var Form = conn.model('Form', form_schemas.formSchema); 

var formCounter = 0; 

Form.find({ 
    archived: null 
}).exec(function (err, forms) { 
    if (err) { 
     console.log(err); 
     process.exit(0); 
    } 
    async.eachSeries(forms, function (form, doneOneForm) { 
     console.log("begin " + formCounter + " form id: " + form.tinyId); 
     var questionCount = 0; 
     var areYouDone = function() { 
      console.log(questionCount); 
      if (questionCount === 0) { 
       form.save(function (err) { 
        if (err) 
         process.exit(1); 
        else { 
         console.log('saved form id: ' + form.tinyId); 
         formCounter++; 
         doneOneForm(); 
        } 
       }); 
      } 
     }; 
     var formElements = form.formElements; 
     var getQuestions = function (formElements) { 
      formElements.forEach(function (fe) { 
       if (fe.elementType === 'question') { 
        questionCount++; 
        var cdeTinyId = fe.question.cde.tinyId; 
        var version = fe.question.cde.version; 
        DataElement.findOne({tinyId: cdeTinyId, version: version}).exec(function (err, cde) { 
         questionCount--; 
         if (err) { 
          console.log(err); 
          process.exit(0); 
         } 
         console.log('found cde id: ' + cdeTinyId + ' version: ' + version); 
         if (cde && cde.ids) fe.question.cde.ids = cde.ids; 
         //if I run this program with this comment instead of above, there is no error, but error happens on the ids which is array of string. 
         //fe.question.cde.name = cde.naming[0].designation; 
         else { 
          console.log("no CDE with id: " + cdeTinyId) 
         } 
         areYouDone(); 
        }); 
       } 
       else { 
        getQuestions(fe.formElements); 
       } 
      }); 
     }; 
     getQuestions(formElements); 
     areYouDone(); 
     form.markModified('formElements'); 
    }, function doneAllForms() { 
     console.log('finished all forms, # form: ' + formCounter); 
     process.exit(0); 
    }); 
}); 
+0

我通過在('data')上使用stream而不是async,stream.pause()來解決此問題。在form.save之後,stream.resume。 – sh977218

回答

0

沒有看到任何從你的日誌語句輸出,我的猜測是你正在進入某種無限遞歸。您到目前爲止顯示的代碼中可能的罪魁禍首是getQuestions(fe.formElements)一行。

formElements屬性指的是它本身(或者以創建循環引用的類似方式引用另一個元素),並且可能第一個值是這樣的,因此它只是一遍又一遍地調用該函數,而沒有forEach()完成。

我想類似的事情也可能發生,如果沒有循環引用,而是從一組formElements下一個鏈接足夠長,造成的問題,並導致getQuestions()進行至少一次爲每個forEach()執行。

您可能希望從較小的表單集合開始,並/或驗證您的fe.elementType值和formElements鏈接/引用是他們應該做的。

相關問題