无法在for循环中实例化多个对象-node.js

时间:2019-05-03 06:23:23

标签: node.js

我正在使用node.js和n-api。要点是,我无法在for循环中创建新对象而没有得到表示以下内容的类型错误:

TypeError: wrappedExamData.getPatientID is not a function

这仅在第二次尝试创建对象时发生。不过,第一次工作正常。如果我在for循环之外创建了多个新对象,那么它也可以正常工作。

这是引发错误的模块

//Require the node module
const nodeModule = require('../../../build/Release/QTWebUI.node');

//Require the Exam Class
const Exam = require('./Exam.js');

exports.retrieveAllExams = function(){
  // Create a js wrapped c++ class to get a list of exam IDs
  var msgGetExamList = new nodeModule.EMGetExamList_Wrapped();
  // Create an array of the examIDs
  var examList = msgGetExamList.getExamIDs();

  console.log(examList);

  var examArray = new Array();

  for(let i = 0; i < examList.length; i++){
    // Create a js wrapped c++ class to send get exam to scanner
    var msgGetExam = new nodeModule.EMGetExam_Wrapped(examList[i]);
    // Get the js wrapped examData from the c++ class
    var wrappedExamData = msgGetExam.getWrappedExamData();
    // Create a new exam object with the wrapped examData
    var exam = new Exam(wrappedExamData);
    // Print the newly created exam object
    console.log("Exam: %s", JSON.stringify(exam)); 
    // Push to array
    examArray.push(exam);
   }

   return examArray;
};

这是我正在使用的Exam类,可追溯到该错误:

module.exports = internal.Exam = class{
  constructor(wrappedExamData){
    this.patientID = wrappedExamData.getPatientID();
    this.firstName = wrappedExamData.getFirstName();
    this.lastName = wrappedExamData.getLastName();
    this.middleName = wrappedExamData.getMiddleName();
    this.gender = wrappedExamData.getGender();
    this.birthDate = wrappedExamData.getBirthDate();
    this.scanTime = wrappedExamData.getScanTime();
    this.description = wrappedExamData.getDescription();
    this.breast = wrappedExamData.getBreast();
    this.accession = wrappedExamData.getAccession();
    this.operatorsName = wrappedExamData.getOperatorsName();
    this.examState = wrappedExamData.getExamState();
    this.progress = wrappedExamData.getProgress();
    this.archiveMultiplicity = wrappedExamData.getArchiveMultiplicity();
    this.density = wrappedExamData.getDensity();
    this.targetType = wrappedExamData.getTargetType();  
    this.fromMWL = wrappedExamData.getFromMWL();
  }
}

如果我像这样实例化对象:

// Create a js wrapped c++ class to send get exam to scanner
var msgGetExam = new nodeModule.EMGetExam_Wrapped(examList[0]);
// Get the js wrapped examData from the c++ class
var wrappedExamData = msgGetExam.getWrappedExamData();
// Create a new exam object with the wrapped examData
var exam = new Exam(wrappedExamData);
// Print the newly created exam object
console.log("Exam: %s", JSON.stringify(exam)); 
// Push to array
examArray.push(exam);

// Create a js wrapped c++ class to send get exam to scanner
var msgGetExam1 = new nodeModule.EMGetExam_Wrapped(examList[1]);
// Get the js wrapped examData from the c++ class
var wrappedExamData1 = msgGetExam1.getWrappedExamData();
// Create a new exam object with the wrapped examData
var exam1 = new Exam(wrappedExamData1);
// Print the newly created exam object
console.log("Exam: %s", JSON.stringify(exam1)); 
// Push to array
examArray.push(exam1);

工作正常。这里有我想念的东西吗?

0 个答案:

没有答案