首页 > 解决方案 > 超级账本作曲家中的查询构建器

问题描述

我正在开发事务中的动态查询。这是我的代码

let queryString = `SELECT org.zcon.healthcare.Patient WHERE(`;
let conditions = [];
if (tx.hasOwnProperty('firstName')) {
    conditions.push(`(firstName == tx.firstName)`)
};
if (tx.hasOwnProperty('lastName')) {
    conditions.push(`(lastName == tx.lastName)`)
};
if (tx.hasOwnProperty('gender')) {
    conditions.push(`(gender == tx.gender)`)
};
if (tx.hasOwnProperty('birthDate')) {
    conditions.push(`(birthDate == tx.birthDate)`)
};
if (tx.hasOwnProperty('ssn')) {
    conditions.push(`(ssn == tx.ssn)`)
};
if (tx.hasOwnProperty('medicalRecordNumber')) {
    conditions.push(`(medicalRecordNumber == tx.medicalRecordNumber)`)
};
queryString += conditions.join(' AND ') + ')';

let query =  buildQuery(queryString);
const searchPatient = await query('query');
if(searchPatient.length ==0){
    throw "No Patient Records found!!"
}else

return searchPatient; 

我在日志中打印了这个查询,它显示为

SELECT org.zcon.healthcare.Patient WHERE ((firstName == Tony) AND (lastName == Stark) AND (gender == M) AND (birthDate == 03/16/1991) AND (ssn == 452896325) AND (medicalRecordNumber == 00001))

但它抛出错误

Expected "!", "(", "[", "false", "function", "new", "null", "this", "true", "{", comment, end of line, identifier, number, regular expression, string or whitespace but "0" found. Line 1 column 123 Line 1 column 123

谁能帮我找出错误。无法破解它。

标签: hyperledger-composer

解决方案


正确构建查询的代码示例:

模型文件

transaction qry1 {
  o Patient patient    // which has all the fields outlined above
}

交易功能

/**
* Sample transaction calling dynamic build query
* @param {org.zcon.healthcare.qry1} qtx1 
* @transaction
*/
async function queryHistory(qtx1) 

{

  const serializer = getSerializer();
  const tx = serializer.toJSON(qtx1); // now an object
  // console.log(' firstname is ' + tx.patient.firstName);  // eg. 'joe'



let queryString = 'SELECT org.zcon.healthcare.Patient WHERE(';
let conditions = [];
  if (tx.patient.hasOwnProperty('firstName')) {
    conditions.push( '(firstName == "' + tx.patient.firstName + '")')
};
if (tx.patient.hasOwnProperty('lastName')) {
    conditions.push( '(lastName == "' + tx.patient.lastName + '")' )
};
if (tx.patient.hasOwnProperty('gender')) {
    conditions.push( '(gender  == "' +  tx.patient.gender + '")' )
};
if (tx.hasOwnProperty('birthDate')) {
    conditions.push(`(birthDate == tx.birthDate)`)
};
if (tx.patient.hasOwnProperty('ssn')) {
    conditions.push( '(ssn == "' + tx.patient.ssn + '")' )
};
if (tx.patient.hasOwnProperty('medicalRecordNumber')) {
    conditions.push( '(medicalRecordNumber == "' + tx.patient.medicalRecordNumber + '")')
} ;

queryString += conditions.join(' AND ') + ')';

let q1 =  buildQuery(queryString);
const searchPatient = await query(q1);


// EXAMPLE OF PRINTING ELEMENTS OF THE RESULT SET (USING CONSOLE.LOG):

if(searchPatient.length ==0){
    throw "No Patient Records found!!"
} else {

   console.log(searchPatient);  // results, array of objects
   console.log('element is ' + searchPatient[0]);   // first object
   console.log('element field is ' + searchPatient[0].medicalRecordNumber); // field
 }


// return searchPatient;  // you do not need this 

} // end function

推荐阅读