[英]how to check condition before updating asset in Hyperledger Fabric
[英]History of asset in Hyperledger Fabric
我正在使用 node.js 編寫鏈碼,我想獲取葯品供應鏈中的葯物歷史。 我部署了鏈碼,調用了制造和購買合同,將葯物的當前狀態從一個所有者修改為另一個所有者。 事實上,我只是為此修改了商業票據鏈碼。 所有者的更改反映在 couchdb 數據庫中。 但是當我嘗試通過葯物密鑰獲取葯物歷史時,它並沒有按預期工作。
我使用的代碼
const promiseOfIterator = this.ctx.stub.getHistoryForKey(drugKey);
const results = [];
for await (const keyMod of promiseOfIterator) {
const resp = {
timestamp: keyMod.timestamp,
txid: keyMod.tx_id
}
if (keyMod.is_delete) {
resp.data = 'KEY DELETED';
} else {
resp.data = keyMod.value.toString('utf8');
}
results.push(resp);
}
return results;
當我打印結果時,它給出: []
而當我這樣做時: Drug.fromBuffer(getDrugHistoryResponse);
並打印出來,它給Drug { class: 'org.medicochainnet.drug', key: ':', currentState: null }
如何使這項工作? 我在這里做錯了什么? 請幫我。
功能
ctx.stub.getHistoryForKey(drugKey);
是一個異步函數。 所以你需要添加等待
const promiseOfIterator = await this.ctx.stub.getHistoryForKey(drugKey);
然后你可以迭代結果。
我在這樣的演示中完成了它:
const promiseOfIterator = await this.ctx.stub.getHistoryForKey(drugKey);
const results = [];
while(true){
let res = await promiseOfIterator.next();
//In the loop you have to check if the iterator has values or if its done
if(res.value){do your actions}
if(res.done){
// close the iterator
await promiseOfIterator.close()
// exit the loop
return results
}
}
查看 Mozilla 文檔以獲取有關 Javascript 中迭代器的更多信息。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.