我的两个函数出现expected resource or concept
错误。根据hyperledger composer API示例,我正在创建Concept和Resource并以正确的方式分配它们的值。
我的代码的作用:
createUser()
User
包含一个名为UserData
的概念,它由四个字符串字段组成。我尝试创建一个新用户,首先创建一个新的UserData
概念并更新其字段,然后将用户的userData设置为这个新概念。
grantAccess()
按ID在注册表中搜索用户,并将其access
值设置为true。
这两个函数抛出Error: Error trying invoke business network. Error: No valid responses from any peers. Response from attempted peer comms was an error: Error: chaincode error (status: 500, message: Error: Expected a Resource or Concept.)
.cto:
namespace org.acme.biznet
concept UserData {
o String name
o String id
o String postcode
o String birthdate
}
participant User identified by userId {
o String userId
o String name
o UserData userData
o Boolean access
}
transaction createUser {
o UserData newUserData
}
transaction grantAccess {
o String userId
}
event NewUserCreated {
o User user
}
event UserAccessRightsChanged {
o User user
o Boolean oldValue
o Boolean newValue
}
的.js:
/**
* Creates a new user.
* @param {org.acme.biznet.createUser} createUser The create user transaction.
* @transaction
*/
function createUser(createUser) {
// create new instance of a User
var factory = getFactory();
var newUser = factory.newResource('org.acme.biznet', 'User', '123');
var newData = factory.newConcept('org.acme.biznet', 'UserData');
newData.name = createUser.newUserData.name;
newData.id = createUser.newUserData.id;
newData.postcode = createUser.newUserData.postcode;
newData.birthdate = createUser.newUserData.birthdate;
newUser.userData = newData;
return getParticipantRegistry('org.acme.biznet.User')
.then(function (userRegistry) {
return userRegistry.update(createUser.user);
})
.then(function () {
// Emit an event for the new user creation.
var event = getFactory().newEvent('org.acme.biznet', 'NewUserCreated');
event.user = newUser;
emit(event);
});
}
/**
* Grants access to the user data.
* @param {org.acme.biznet.grantAccess} userGrantAccess The grantAccess transaction.
* @transaction
*/
function grantAccess(userGrantAccess) {
var existingAccessValue;
var theUser;
// Get the user from the registry given the id
getParticipantRegistry('org.acme.biznet')
.then(function (participantRegistry) {
// Get the specific User from the participant registry.
return participantRegistry.get(userGrantAccess.userId);
})
.then(function (user) {
theUser = user;
existingAccessValue = user.access;
user.access = true;
});
// Get and update the user registry
return getParticipantRegistry('org.acme.biznet.User')
.then(function (userRegistry) {
return userRegistry.update(theUser);
})
.then(function () {
// Emit an event for the modified user rights.
var event = getFactory().newEvent('org.acme.biznet', 'UserAccessRightsChanged');
event.user = theUser;
event.oldValue = existingAccessValue;
event.newValue = theUser.access;
emit(event);
});
}
答案 0 :(得分:0)
此简化功能(基于Bond Sample网络)在Composer Playground中运行 - 使用修改后的模型。
/**
* Creates a New User
* @param {org.acme.biznet.CreateUser} cUser
* @transaction
*/
function createUser(cUser) {
console.log(cUser)
return getParticipantRegistry('org.acme.biznet.User')
.then(function (registry) {
var factory = getFactory();
// Create the user
var user = factory.newResource('org.acme.biznet', 'User', cUser.newuserId);
user.name = cUser.newname;
user.userData = cUser.newuserData;
user.access = cUser.newaccess;
// Add the bond asset to the registry.
return registry.add(user);
});
}

修改后的模型:
namespace org.acme.biznet
concept UserData {
o String name
o String id
o String postcode
o String birthdate
}
participant User identified by userId {
o String userId
o String name
o UserData userData
o Boolean access
}
transaction CreateUser {
o String newuserId
o String newname
o UserData newuserData
o Boolean newaccess
}
transaction grantAccess {
o String userId
}
event NewUserCreated {
o User user
}
event UserAccessRightsChanged {
o User user
o Boolean oldValue
o Boolean newValue
}