hyperledger-composer - 超级账本作曲家中的查询构建器
问题描述
我正在开发事务中的动态查询。这是我的代码
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
谁能帮我找出错误。无法破解它。
解决方案
正确构建查询的代码示例:
模型文件
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
推荐阅读
- javascript - 如何在本机反应中在输入字段的右侧添加按钮
- python-multiprocessing - 锁在多处理中实际上做了什么?
- c - 想在 C 的下一个(for)循环中使用一个变量
- arrays - 数组数组中的所有组合
- c# - 在不使用主页面的情况下在详细信息页面中导航
- import - SAS Proc Import“Table”:“Table”参数是必需的参数吗?
- jquery - Ajax 请求后选择选项未重置
- matlab - Matlab if else循环
- php - 使用 Wordpress 为 ShareTribe 的托管版本创建页面
- android - 在 Kivy 中绑定 'esc' 并在 kv 文件中完成屏幕管理