我正在尝试创建一批项目(单位)。我希望用户在一个批处理中指示他们想要的单位数,然后事务首先创建该批处理,然后创建所需的单位数。这是我的代码:
async function createBatch(batchTx) {
// get a code from the generator
let now = new Date();
let tokenData = {
brand: batchTx.brand,
unitCount: batchTx.unitCount,
created: now,
expiry: batchTx.expiryDate
}
let code = _generate_code(tokenData, 'Batch');
// create a new Batch token and add it to the registry
let factory = getFactory();
let token = factory.newResource('org.myOrganization', 'Token', String(code));
token.created = now;
token.updated = now;
let tokenAssetRegistry = await getAssetRegistry('org.myOrganization.Token');
await tokenAssetRegistry.add(token);
// create a batch using the token and code created above
let batch = factory.newResource('org.myOrganization', 'Batch', token.code);
batch.brand = batchTx.brand;
batch.expiryDate = batchTx.expiryDate;
batch.token = token;
batch.owner = batchTx.owner;
batch.created = now;
batch.updated = now;
let batchAssetRegistry = await getAssetRegistry('org.myOrganization.Batch');
await batchAssetRegistry.add(batch);
// update token with new batch
let tokenAssetRegistry1 = await getAssetRegistry('org.myOrganization.Token');
token.batch = batch;
tokenAssetRegistry1.update(token);
// CREATE UNITS
// get a code from the generator
let i;
for(i=0; i < batchTx.unitCount; i++) {
let unitTokenData = {
batch: batch,
created: now
};
let unitCode = _generate_code(unitTokenData, 'Unit');
// create a new Unit token and add it to the registry
let unitToken = factory.newResource('org.myOrganization', 'Token', String(unitCode));
unitToken.created = now;
unitToken.updated = now;
let tokenAssetRegistry2 = await getAssetRegistry('org.myOrganization.Token');
await tokenAssetRegistry2.add(unitToken);
// create units
let unit = factory.newResource('org.myOrganization', 'Unit', String(unitToken.code));
unit.batch = batch;
unit.token = unitToken;
unit.owner = batchTx.owner;
unit.created = now;
unit.updated = now;
let unitAssetRegistry = await getAssetRegistry('org.myOrganization.Unit');
await unitAssetRegistry.add(unit);
}
问题是批处理创建正常,但是当涉及到单位时,如果 batchTx.unitCount
为 3,它不会创建 3 个单位,它只会创建一个。 Composer 的工作方式有什么问题吗,或者我的某些 promise 解决方案是错误的?解决此问题的任何帮助将不胜感激
最佳答案
(更新):
您不能(在同一个交易中 - 上面)
更新
一个新的token
Assets (在之前的add
之后) (add
) 尚未提交到账本。但是您可以简单地等待批处理“添加”完成,然后执行一个单数操作:let tokenAssetRegistry = await getAssetRegistry('org.myOrganization.Token'); await tokenAssetRegistry.add(token);
您的代码在使用
new Date()
时是不确定的 - 它在背书同行之间不会相同(得到不同的结果)。看不到您是否在其他地方“削减”日期字符串(以减少不确定性因素)。为什么不使用“交易时间戳”(下面的链接)。同样的不确定性适用于代码的
unitToken
部分。关于非确定性——建议看这个获取确定性日期时间的例子。 Getting timestamps in deterministic way in Hyperledger Composer transactions我建议阅读这篇关于如何处理 for 循环中的 promise 的文章 -> https://medium.com/@antonioval/making-array-iteration-easy-when-using-async-await-6315c3225838管理返回的 promise (我想是的,这就是你的循环没有完成的原因)|。您的代码效率低下,如果它有任何值(value),您可以创建
units
作为资源数组,然后使用一个单一的unitAssetRegistry.addAll()
- 在 for 循环之后完成。这方面的一个例子是在这个示例网络中 -> https://github.com/hyperledger/composer-sample-networks/blob/master/packages/fund-clearing-network/lib/clearing.js#L191 (在这种情况下使用updateArray
,以及注册表.updateAll()
方法)。
关于javascript - Hyperledger composer 在 for 循环中创建多个 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51515888/