为基于 Hyperledger Fabric v0.6 的区块链网络开发客户端应用程序(11)
- UID
- 1066743
|
为基于 Hyperledger Fabric v0.6 的区块链网络开发客户端应用程序(11)
从区块链获取抵押贷款申请这是 getMortgageApplication 方法的代码段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| function getMortgageApplication(params) {
return new Promise(function(resolve, reject){
try{
logHelper.logEntryAndInput(logger, 'getMortgageApplication', params);
if(!validate.isValidJson(params)){
logHelper.logError(logger, 'getMortgageApplication', 'Invalid params');
return reject({statusCode: constants.INVALID_INPUT, body: 'Could not fetch mortgage application. Invalid params' })
}
var user = params.user;
if(!validate.isValidString(user)){
logHelper.logError(logger, 'getMortgageApplication', 'Invalid user');
return reject({statusCode: constants.INVALID_INPUT, body: 'Could not fetch mortgage application. Invalid user' })
}
var id = params.id;
if(!validate.isValidString(id)){
logHelper.logError(logger, 'getMortgageApplication', 'Invalid id');
return reject({statusCode: constants.INVALID_INPUT, body: 'Could not fetch mortgage application. Invalid id' })
}
var reqSpec = getRequestSpec({functionName: 'GetLoanApplication', args: [id]});
recursiveQuery({requestSpec: reqSpec, user: user})
.then(function(resp){
logHelper.logMessage(logger, 'GetMortgageApplication', 'Successfully fetched mortgage application', resp.body);
return resolve({statusCode: constants.SUCCESS, body: resp.body});
})
.catch(function(err){
logHelper.logError(logger, 'GetMortgageApplication', 'Could not fetch mortgage application', err);
return reject({statusCode: constants.INTERNAL_SERVER_ERROR, body: 'Could not fetch mortgage applications' });
});
}
catch(err){
logHelper.logError(logger, 'getMortgageApplication', 'Could not fetch property ad ', err);
return reject({statusCode: constants.INTERNAL_SERVER_ERROR, body: 'Could not fetch mortgage application ' });
}
});
}
|
getMortgageApplication 方法在结构上类似于 createMortgageApplication 方法。它接受需要代表其调用链代码的用户,以及要获取的抵押贷款申请的 ID。
随后是 getRequestSpec 方法,该方法像之前一样创建请求规范并调用 recursiveQuery 方法来查询区块链。
使用链代码查询区块链这是 doQuery 方法的代码段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| var chain = bcNetwork.getChain();
var chainAsync = Promise.promisifyAll(chain);
chainAsync.getMemberAsync(user)
.then(function(member){
var tx = member.query(requestSpec);
tx.on('submitted', function() {
logHelper.logMessage(logger, 'doQuery','Transaction for query submitted');
});
tx.on('complete', function(data) {
try{
logHelper.logMessage(logger, 'doQuery', 'Transaction for query complete ',requestSpec);
var buffer = new Buffer(data.result);
var jsonResp = JSON.parse(buffer.toString());
return resolve({statusCode: constants.SUCCESS, body: jsonResp});
}
catch(err){
logHelper.logError(logger,'doQuery','Could not parse query response',err);
return reject({statusCode: constants.INTERNAL_SERVER_ERROR, body: 'Could not parse query response ' });
}
});
tx.on('error', function (err) {
logHelper.logError(logger, 'doQuery', 'Could not perform query ',err);
return reject({statusCode: constants.INTERNAL_SERVER_ERROR, body: 'Could not perform query ' });
});
})
|
- 第 7 行将对成员对象调用 query 方法,并传入合适的请求规范。
在区块链上成功执行查询事务后,将调用 complete 事件。从区块链读取的数据需要读入到一个缓冲区中。因为我们的链代码实现将数据存储为 JSON 字符串,所以需要将返回的内容解析为 JSON。 |
|
|
|
|
|