向现有的关系数组中添加新的关系

时间:2018-07-18 11:48:20

标签: hyperledger-composer

我的一项资产中有很多关系。

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);

如何更新资产?

1 个答案:

答案 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);