当标识符为userId时如何通过电子邮件ID从注册表中获取参与者

时间:2018-10-30 15:08:04

标签: hyperledger-fabric hyperledger hyperledger-composer

我正在通过创建事务名称createUser添加参与者。我的参与者是由我使用uuid()随机生成的用户ID标识的

您可以在此处看到我的用户参与者模型和交易。

participant User identified by userId {
 o String userId
 o String name
 o String email 
 o Strign password
 o String isVerified
} 

transaction createUser {
 o String userId
 o String name
 o String email 
 o Strign password
 o String isVerified
}

当我使用电子邮件作为标识符时,同时添加两个具有相同电子邮件ID的用户。这会引发错误>>用户已经存在,并且具有相同电子邮件ID。 但在我国,大多数用户没有电子邮件。因此,我决定使用随机的userId创建用户。

问题是选择userId作为标识符,我无法检查之前是否已注册电子邮件ID。

我的事务逻辑代码在这里,供您更好地理解。

const model = 'org.composer.app'

//generating random userId
function uuid() {
    const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
}

//create User Transaction logic
async function createUser (tx){
  const userRegistry = await getParticipantRegistry(model+'.user.User')
  const exist = await userRegistry.exists('tx.email')
  if(exist){
    throw new Error("already exist")
  }else{
   const user = getFactory().newResource(model+'.user','User',uuid())
  user.name = tx.name
  user.email = tx.email
  user.password = tx.password
  user.isVerified = tx.isVerified
  await userRegistry.add(user)
 }
} 

===============================

根据评论和回答,我正在更新帖子。 我认为电子邮件应该是资产而不是资产,但无论如何,人们都说电子邮件应该是资产,所以我将其保留为资产

asset Email identified by email {
o String email
}

participant User identified by userId {
 o String userId
 o String name
 --> Email email 
 o Strign password
 o String isVerified
} 

transaction createUser {
 o String userId
 o String name
 o String email 
 o Strign password
 o String isVerified
}

const model = 'org.composer.app'

//generating random userId
function uuid() {
    const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
}

//create User Transaction logic
async function createUser (tx){
  const userRegistry = await getParticipantRegistry(model+'.user.User')
   const emailRegistry = await getAssetRigistry(model+'.email','Email')
   const exist =  emailRegistry.exist(tx.email)
   if (exist){
  throw new Error('Email already exist')
  }else{
  const user = getFactory().newResource(model+'.user','User',uuid())
  user.name = tx.name
  user.email = tx.email
  user.password = tx.password
  user.isVerified = tx.isVerified
   await userRegistry.add(user)
 }
}

1 个答案:

答案 0 :(得分:1)

您可以在此处使用查询

const model = 'org.composer.app'

//generating random userId
function uuid() {
    const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
}

//create User Transaction logic
async function createUser (tx){

  const userRegistry = await getParticipantRegistry(model+'.user.User')

  let mailQuery = buildQuery('SELECT org.composer.app.User WHERE (email == _$inputValue)');

  let assets =  await query(mailQuery, { inputValue: tx.email });

  // if assets.length != 0 that means mail id already exist

  if(assets.length != 0){
    throw new Error("email already exist");
  }else{

   const user = getFactory().newResource(model+'.user','User',uuid());
   user.name = tx.name;
   user.email = tx.email;
   user.password = tx.password;
   user.isVerified = tx.isVerified;
   await userRegistry.add(user);

 }
}