我的一项资产中有很多关系。
asset Patient identified by medicalRecordNumber {
--> LabReports[] labReports optional
}
在事务中,我试图在数组labReports中添加新的关系。但是它给我的错误是TypeError: Cannot read property 'push' of undefined
这是我的logic.js代码
var patientId = searchPatient[0].medicalRecordNumber;
var patientRegistry = await getAssetRegistry('org.zcon.healthcare.Patient');
var patientObj = await patientRegistry.get(patientId);
var labreports=[];
labreports=patientObj.labReports;
var newLabReport =factory.newResource('org.zcon.healthcare', 'LabReports', labReportFromattedId);
labreports.push(newLabReport);
patientObj.labReports=labreports;
await patientRegistry.update(patientObj);
如何更新资产?
答案 0 :(得分:1)
最初,labReports
没有任何patientObj
,因此它是未定义的,并且当您尝试访问push
属性的undefined
方法时,会引发错误。
尝试以下解决方案,它应该可以工作
var patientId = searchPatient[0].medicalRecordNumber;
var patientRegistry = await getAssetRegistry('org.zcon.healthcare.Patient');
var patientObj = await patientRegistry.get(patientId);
if(typeof patientObj.labReports !== 'object' || patientObj.labReports.constructor !== Array) {
patientObj.labReports = [];
}
var newLabReport = factory.newResource('org.zcon.healthcare', 'LabReports', labReportFromattedId);
patientObj.labReports.push(newLabReport);
await patientRegistry.update(patientObj);