正在尝试创建一批项目(单位)。我希望用户指示他们想要的批次数量,然后事务首先创建批次,然后创建所需的数量。这是我的代码:
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个单位,而只会创建一个。作曲家的工作方式有什么问题吗,或者我的某些诺言解析是错误的?解决该问题的任何帮助将不胜感激
答案 0 :(得分:2)
(已更新):
您(在上述同一笔交易中)不能update
拥有token
之后的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循环中的诺言的内容-> https://medium.com/@antonioval/making-array-iteration-easy-when-using-async-await-6315c3225838以管理返回的诺言(我认为是的,这就是您的循环未完成的原因)|。您的代码效率低下,如果具有任何价值,则可以将units
创建为资源数组,然后使用一个单数unitAssetRegistry.addAll()
-在for循环完成之后。一个示例在此示例网络中-> https://github.com/hyperledger/composer-sample-networks/blob/master/packages/fund-clearing-network/lib/clearing.js#L191(在这种情况下使用updateArray
,并且使用注册表.updateAll()
方法)。