gpt4 book ai didi

hyperledger-fabric - Hyperledger-composer:预期的资源或概念

转载 作者:行者123 更新时间:2023-12-04 07:54:06 26 4
gpt4 key购买 nike

我在使用两个函数时遇到了expected resource or concept 错误。根据 hyperledger composer API 示例,我正在创建概念和资源并以正确的方式分配它们的值。

我的代码做了什么:

createUser()
User 包含一个名为 UserData 的概念,它由四个字符串字段组成。我尝试通过首先创建一个新的 UserData 概念并更新其字段,然后将用户的 userData 设置为这个新概念来创建一个新用户。

grantAccess()
按 ID 在注册表中搜索用户并将其 access 值设置为 true。

两个函数都抛出 错误:尝试调用业务网络时出错。错误:没有来自任何同行的有效响应。来自尝试的对等通信的响应是一个错误:错误:链代码错误(状态:500,消息:错误:需要资源或概念。)

.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);
});
}

最佳答案

此简化函数(基于 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
}

关于hyperledger-fabric - Hyperledger-composer:预期的资源或概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49259180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com